最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

課后練習(xí)的問題。求指導(dǎo)!

我不知道怎么把數(shù)據(jù)帶進(jìn)去...


1. 這個(gè)是父類的abstract。

package com.imooc;


public abstract class Shape {

? ? public abstract void perimeter(); ? ?

? ? public abstract void area();

}

2. 然后是子類的Rectangle。嘗試了一下失敗了。

package com.imooc;


public class Rectangle extends Shape {


Rectangle p1 = new Rectangle();

private int periSum(){

int length = 10;

int width = 5;

int pSum = (length + width) *2;

return pSum;

}


@Override

public void perimeter() {

// TODO Auto-generated method stub

? ? ?System.out.println("圓形的周長(zhǎng)是:"+ p1.periSum());

}



@Override

public void area() {

// TODO Auto-generated method stub

? ? ?System.out.println("圓形的面積是:");

}


}

3. 接著是子類的Circle。

package com.imooc;


public class Circle extends Shape {


@Override

public void perimeter() {

// TODO Auto-generated method stub

? ? ? ?System.out.println("長(zhǎng)方形的周長(zhǎng)是:");

}


@Override

public void area() {

// TODO Auto-generated method stub

? ? ?System.out.println("長(zhǎng)方形的面積是:");

}


}

4. 最后是Initial。

package com.imooc;


public class Initial {


public static void main(String[] args) {

// TODO Auto-generated method stub

? ? //計(jì)算周長(zhǎng)

Shape per1 = new Rectangle();

? ? Shape per2 = new Circle();

? ? //求長(zhǎng)方形的周長(zhǎng)

? ? per1.perimeter();

? ? //求圓的周長(zhǎng)

? ? per2.perimeter();

? ? //計(jì)算面積

? ? Shape ar1 = new Rectangle();

? ? Shape ar2 = new Circle();

? ? ar1.area();

? ? ar2.area();

}


}




正在回答

1 回答

你在

public?class?Rectangle?extends?Shape?{

Rectangle?p1?=?new?Rectangle();
private?int?periSum(){
int?length?=?10;
int?width?=?5;
int?pSum?=?(length?+?width)?*2;
return?pSum;
}

建立了對(duì)象,然后又在initial里建立了對(duì)象,這樣使用有問題

public?void?perimeter()?{
//?TODO?Auto-generated?method?stub
?????System.out.println("圓形的周長(zhǎng)是:"+?p1.periSum());
}

你在initial中建立的對(duì)象沒有去調(diào)用perimeter方法,因?yàn)槟氵@個(gè)方法是使用p1這個(gè)對(duì)象調(diào)用periSum,建議你把Rectangel中的Rectangle p1 = new Rectangle();注釋掉,然后把p1改成this,或者直接使用periSum這個(gè)方法。

你這里的主要問題就是你在initial中建立的對(duì)象,沒有真正的用來調(diào)用periSum這個(gè)方法,實(shí)際調(diào)用這個(gè)方法的。另外還有一個(gè)問題,你在方法內(nèi)部建立的變量并不是成員變量,這個(gè)變量并不會(huì)被你建立的方法是用。

所以你應(yīng)該把int length,int width寫在方法體外面,讓他成為類成員變量。建議好好看看有關(guān)java類的內(nèi)容。本人也是菜鳥,如有錯(cuò)誤請(qǐng)見諒。

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

ilemon 提問者

好暈。。。。。雖然照你說的改對(duì)了。。。但是。。。后面你在說什么0.0
2015-05-19 回復(fù) 有任何疑惑可以回復(fù)我~
#2

apprentice1104 回復(fù) ilemon 提問者

主要就是你對(duì)class的使用和理解不太熟悉,如果我說的是對(duì)的那么能麻煩你給個(gè)最優(yōu)答案然后結(jié)貼嗎?
2015-05-19 回復(fù) 有任何疑惑可以回復(fù)我~
#3

ilemon 提問者 回復(fù) apprentice1104

可是還是不明白...
2015-05-19 回復(fù) 有任何疑惑可以回復(fù)我~
#4

apprentice1104 回復(fù) ilemon 提問者

不明白就看看有關(guān)class的內(nèi)容吧,你不能指望發(fā)這個(gè)帖子就弄清楚所有的知識(shí),而且我覺得我已經(jīng)講的很詳細(xì)了。
2015-05-19 回復(fù) 有任何疑惑可以回復(fù)我~
#5

ilemon 提問者

非常感謝!
2015-05-19 回復(fù) 有任何疑惑可以回復(fù)我~
查看2條回復(fù)

舉報(bào)

0/150
提交
取消
Java入門第二季
  • 參與學(xué)習(xí)       531268    人
  • 解答問題       6327    個(gè)

課程升級(jí)!以終為始告別枯燥,在開發(fā)和重構(gòu)中體會(huì)Java面向?qū)ο缶幊痰膴W妙

進(jìn)入課程

課后練習(xí)的問題。求指導(dǎo)!

我要回答 關(guān)注問題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)