package q4;
public class TestAnimal {
public static void main(String[] args) {
class Animal {
void shout() {
System.out.println("动物都会发出叫声");
}
}
class Dog extends Animal {
String name;
public Dog(String name) {
this.name = name;
}
void shout() {
System.out.println("汪汪汪");
}
}
class Cat extends Animal {
String name;
public Cat(String name) {
this.name = name;
}
void shout() {
System.out.println("喵喵喵");
}
}
Dog qq = new Dog("大黄");
System.out.printf(qq.name);
qq.shout();
Cat yy = new Cat("小花");
System.out.printf(yy.name);
yy.shout();
}
}
package q4;
public class TestShape {
public static void main(String[] args) {
Rectangle zheng=new Rectangle(3,4);
System.out.print("矩形面积"+String.format("%-20s", zheng.area()));
System.out.println("矩形周长"+String.format("%-20s",zheng.perimeter()));
Round yuan=new Round(3);
System.out.print("圆形面积"+String.format("%-20s",yuan.area()));
System.out.println("圆形周长"+String.format("%-20s",yuan.perimeter()));
}
}
abstract class Shape{
abstract double area();
abstract double perimeter();
}
class Round extends Shape{
double r;
public Round(double r){
this.r=r;
}
double area(){
return Math.PI*r*r;
}
double perimeter(){
return 2*Math.PI*r;
}
}
class Rectangle extends Shape{
double length,width;
public Rectangle(double length,double width){
this.length=length;
this.width=width;
}
double area(){
return length*width;
}
double perimeter(){
return 2*(length+width);
}
}