请求高手帮忙改写下一个java程序

2024-12-21 06:08:36
推荐回答(3个)
回答1:

你这题好象有点问题啊 自类是不能继承父类private的成员的 定义成private的话就不能继承了
你要定义的话只能是protected的
还有你这个把好多方法定义在了子类里这么做的话就完全失去了多态的意义

--------------------------------------------------------------------
class TwoDShape{
double width;
double height;
String style;
double area(){
return width*height/2;
}
void showStyle(){
System.out.println("Triangle is " + style);
}

void showDim (){
System.out.println("Width and height are "+width+" and "+height);
}
}
class Triangle extends TwoDShape{

}
------------------------------------------------------------
这么写就好多了

回答2:

不算太了解你的需求,而且没有IDE环境。我做了如下修改,你看看吧。合用不。

class TwoDShape{
private double width;
private double height;

public TwoDShape(double width, double height) {
this.width = width;
this.height = height;
}

public void showDim (){
System.out.println("Width and height are "+width+" and "+height);
}

public double getWidth() {
return(this.width);
}

public double getHeight() {
return(this.height);
}

public void setWidth(double width) {
this.width = width;
}

public void setHeight(double height) {
this.width = height;
}
}
class Triangle extends TwoDShape{
String style;

public Triangle(double width, double height, String style) {
super(width, height);
this.style = style;
}

public double area(){
return width*height/2;
}

public void showStyle(){
System.out.println("Triangle is " + style);
}
}
class Rectangle extends Triangle{
public Rectangle(double width, double height, String style) {
super(width, height, style);
}

public double line(){
return width + height;
}
}

class Shapes{
public static void main(String args[]){
Triangle t1=new Rectangle(4.0, 4.0, "isosceles");
Triangle t2=new Rectangle(8.0, 12.0, "right");
System.out.println("Info for t1: ");
t1.showStyle();
t1.showDim();
System.out.println("Area is " +t1.area());
System.out.println("Line is " +t1.line());
System.out.println();
System.out.println("Info for t2: ");
t2.showStyle();
t2.showDim();
System.out.println("Area is " +t2.area());
System.out.println("Line is " +t2.line());
}
}

回答3:

class TwoDShape{
private double width;///声明为私有变量
private double height;

public double getWidth() {///定义一些列的访问器
return width;
}

public void setWidth(double width) {
this.width = width;
}

public double getHeight() {
return height;
}

public void setHeight(double height) {
this.height = height;
}

void showDim (){
System.out.println("Width and height are "+width+" and "+height);
}
}
class Triangle extends TwoDShape{
String style;
double area(){
return getWidth()*getHeight()/2;
}
void showStyle(){
System.out.println("Triangle is " + style);
}
}

class rectangle extends TwoDShape{///添加子类rectangle,包括计算面积和周长的方法
String style;
double banjing;
double showZhouchang(){
return 2*3.14159*banjing;
}
double showMianji(){
return 3.14159*banjing*banjing;
}
double showBanJing(){
return banjing;
}
void showStyle(){
System.out.println("Triangle is " + style);
}
}

class Shapes{
public static void main(String args[]){
Triangle t1=new Triangle();
Triangle t2=new Triangle();
rectangle t3=new rectangle();
t1.setWidth(4.0);
t1.setHeight(4.0);
t1.style="isosceles";
t2.setWidth(8.0);
t2.setHeight(12.0);
t2.style="right";
t3.style="yuan";
t3.banjing=5.0;
System.out.println("Info for t1: ");
t1.showStyle();
t1.showDim();
System.out.println("Area is " +t1.area());
System.out.println();
System.out.println("Info for t2: ");
t2.showStyle();
t2.showDim();
System.out.println("Area is " +t2.area());
System.out.println();
System.out.println("Info for t3: ");////调用子类并显示结果。
t3.showStyle();
System.out.println("banjin is "+t3.showBanJing());
System.out.println("mianji is " +t3.showMianji());
System.out.println("zhouchang is " +t3.showZhouchang());
}
}