Java第七课课后作业
-
某个学校培训学生的演讲,要求按照一定的话术去做演讲 A.sayHello() B.show() C.sayBay() 三个话术必须要有,且顺序不能变,具体内容由同学们自由发挥,将上述要求用程序来设计并实现。
public abstract class School {
public abstract void sayHello();
public abstract void show();
public abstract void sayBay();
public void sequence() {
sayHello();
show();
sayBay();
}
}
class Tanyi extends School {
-
instance of 是一个运算符,判断某一个变量是否属于某个类型,返回boolean 设计Rectangle,Suqare,Shape,TestShape类,Shape类是父类,Rectangle,Suqare是子类 要求TestShape类中有计算矩形周长的方法perimether,方法入参是Shape类型 方法中判断入参是哪种具体类型,采用不同的计算周长的方法,方法返回int
public class Shape {
}
class Square extends Shape{
private int length;
public Square(int length) {
this.length = length;
}
public int getLength() {
return length;
}
}
class Rectangle extends Shape{
private int length;
private int width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public int getLength() {
return length;
}
public int getWidth() {
return width;
}
}
class TestShape{
public int perimether(Shape shape) {
if (shape instanceof Square) {
return ((Square) shape).getLength()*4;
} else if (shape instanceof Rectangle) {
return (((Rectangle) shape).getLength() + ((Rectangle) shape).getWidth()) * 2;
} else {
System.out.println("此方法只能计算矩形和正方形的周长");
return 0;
}
}
public static void main(String[] args) {
Shape s = new Square(4);
TestShape t = new TestShape();
System.out.println("正方形的周长为:"+t.perimether(s));
}
}
软件下载提取码:qwer