亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

代碼有點小毛病,不能分開賦值,求大佬指點

父類

package project55;

public abstract class Shape {

? public abstract void c(int a,int b,int r);

? public abstract void s(int a,int b,int r);

}

矩形

package project55;


public class Rectangle extends Shape {


@Override

public void c(int a,int b,int r) {

// TODO Auto-generated method stub

? ? ? ? System.out.println("矩形周長:"+(a+b)*2);

}


@Override

public void s(int a,int b,int r) {

// TODO Auto-generated method stub

? ? ? ? System.out.println("矩形面積:"+a*b);

}


}

圓形

package project55;


public class Circle extends Shape {

? ? double π=3.14;

@Override

public void c(int a,int b,int r) {

// TODO Auto-generated method stub

? ? ? ? System.out.println("圓形的周長:"+π*r*2);

}


@Override

public void s(int a,int b,int r) {

// TODO Auto-generated method stub

? ? ? ? System.out.println("圓形的面積:"+π*r*r);

}


}

Initail函數

package project55;

import java.util.Scanner;

public class Initail {


public static void main(String[] args) {

// TODO Auto-generated method stub

? ? ? ? Scanner input=new Scanner(System.in);

System.out.print("請輸入矩形的長a:");

int a=input.nextInt();

? ? ? ? System.out.print("請輸入矩形的寬b:");

? ? ? ? int b=input.nextInt();

? ? ? ? System.out.print("請輸入圓形半徑r:");

? ? ? ? int r=input.nextInt();

? ? ? ? Shape shape1=new Rectangle();

? ? ? ? shape1.c(a,b,r);

? ? ? ? shape1.s(a,b,r);

? ? ? ? Shape shape2=new Circle();

? ? ? ? shape2.c(a,b,r);

? ? ? ? shape2.s(a,b,r);

}


}

結果

請輸入矩形的長a:4

請輸入矩形的寬b:5

請輸入圓形半徑r:6

矩形周長:18

矩形面積:20

圓形的周長:37.68

圓形的面積:113.03999999999999



正在回答

2 回答

你的方法參數寫錯了。正方形的參數傳兩個,圓形的傳一個就可以了。main方法中,調用類方法時,用相應的子類去聲明對象,調用本類的方法。一下是我的代碼,可運行


package j2ee;

import java.util.Scanner;

public class HelloWord{
?? ? public static void main(String[] args) {
?? ? Scanner input = new Scanner(System.in);
?? ? System.out.print("請輸入矩形的長a:");
?? ? int a = input.nextInt();
?? ? System.out.print("請輸入矩形的寬b:");
?? ? int b = input.nextInt();
?? ? System.out.print("請輸入圓形的半徑r");
?? ? int r = input.nextInt();
?? ?
?? ? rectangle s1 = new rectangle();
?? ? s1.c(a,b);
?? ? s1.s(a,b);
?? ?
?? ? circle s2 = new circle();
?? ? s2.c(r);
?? ? s2.s(r);
?? ? }
}
?? ??? ?
?? ?
package j2ee;

public abstract class shape {
?public void r(){};
?public void s(){};
}

package j2ee;
import java.util.Scanner;
public class rectangle extends shape {
? public void c(int a,int b){
?? ?? System.out.println(2*(a+b));
? }
? public void s(int a,int b){
?? ?? System.out.println(a*b);
? }
?
}

?? ?? ?
?package j2ee;

public class circle extends shape {
?? ?double π=3.14;
?? ?public void c(int r){
?? ??? ?? System.out.println(2*π*r);
?? ?? }
?? ?? public void s(int r){
?? ?? System.out.println(π*r*r);
?? ?? }
}


1 回復 有任何疑惑可以回復我~
#1

慕后端1338084

如果不懂可以給我回復,我看到就回
2018-08-21 回復 有任何疑惑可以回復我~
#2

慕慕3418381 提問者 回復 慕后端1338084

為什么主函數方法前面不加abstract 有點不理解
2018-08-25 回復 有任何疑惑可以回復我~
#3

寶慕林9160084

為什么去掉子類中的@Override。感覺去掉之后就和繼承沒有半毛錢關系了,只是在字函數中重新定義了一個函數。不是重載,是重寫
2018-08-27 回復 有任何疑惑可以回復我~

小白的意見,錯了請見諒..

可以先選擇要計算的圖形形狀,用switch判斷下,然后創建相應的實例...

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Java入門第二季 升級版
  • 參與學習       531146    人
  • 解答問題       6326    個

課程升級!以終為始告別枯燥,在開發和重構中體會Java面向對象編程的奧妙

進入課程

代碼有點小毛病,不能分開賦值,求大佬指點

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號