继承、多态(9.22)——————————课后练习
package 课后练习; /* 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。 */ //父类 class Vehicle { //成员属性 private int wheels ; private double weight ; public int getWheels() { return wheels; } public void setWheels(int wheels) { this.wheels = wheels; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } //构造方法 public Vehicle( ) { } //成员方法 public void speak( ) { System.out.println("车轮数量"+wheels+"车重"+weight); } } //子类 class Car extends Vehicle { //成员属性 private int loader ; public int getLoader() { return loader; } public void setLoader(int loader) { this.loader = loader; } //构造方法 public Car( ) { } //成员方法 public void speak1( ) { System.out.println("载人数"+loader); } } //子类 class Truck extends Car { //成员属性 private int payload ; public int getPayload() { return payload; } public void setPayload(int payload) { this.payload = payload; } //构造方法 public Truck( ) { } //成员方法 public void speak2( ) { System.out.println("载重量"+payload); } } //测试类 public class CeshiCar { public static void main(String[] args) { Truck a = new Truck( ) ; a.setLoader(30); a.setPayload(50); a.setWeight(10); a.setWheels(4); a.speak(); a.speak1(); a.speak2(); } }
package 课后练习; /* 编写一个Shape类,具有属性:周长和面积; 定义其子类三角形和矩形,分别具有求周长的方法。 定义主类,在其main方法中创建三角形和矩形类的对象, 并赋给Shape类的对象a、b,使用对象a、b来测试其特性。 */ //父类(抽象类) abstract class Shape { //成员属性 private double zhouchang ; private double mianji ; public double getZhouchang() { return zhouchang; } public void setZhouchang(double zhouchang) { this.zhouchang = zhouchang; } //构造方法 public Shape( ) { } //计算周长方法 public abstract double zc ( ) ; } //子类 class Sanjiaoxing extends Shape { private double a ; private double b ; private double c ; public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public double getC() { return c; } public void setC(double c) { this.c = c; } public double zc ( ) { if ( a+b>c && Math.abs(a-b)<c) { return a+b+c ; } else { return -1 ; } } } //子类 class Juxing extends Shape { private double chang ; private double kuan ; public double getChang() { return chang; } public void setChang(double chang) { this.chang = chang; } public double getKuan() { return kuan; } public void setKuan(double kuan) { this.kuan = kuan; } public Juxing( ) { } public Juxing( double chang,double kuan ) { this.chang = chang; this.kuan = kuan; } public double zc( ) { return (chang+kuan)*2 ; } } //主类 public class CeshiShape { public static void main(String[] args) { Juxing s = new Juxing( ) ; s.setChang(2); s.setKuan(2); System.out.println(s.zc()); Sanjiaoxing x = new Sanjiaoxing( ) ; x.setA(3); x.setB(4); x.setC(5); if(x.zc()==-1) { System.out.println("不是三角形"); } else { System.out.println(x.zc()); } Sanjiaoxing y = new Sanjiaoxing( ) ; y.setA(1); y.setB(1); y.setC(2); if(y.zc()==-1) { System.out.println("不是三角形"); } else { System.out.println(x.zc()); } } }
package 课后练习; /* 定义类Human,具有若干属性和功能;定义其子类Man、Woman; 在主类Test中分别创建子类、父类和上转型对象,并测试其特性。 */ //父类 class Human { //成员属性 private String name ; private int age ; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void hello( ) { System.out.println(name+"-"+age); } } //子类 class Man extends Human { //成员属性 private String sex ; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public void hello1( ) { System.out.println(sex); } } //子类 class Woman extends Human { //成员属性 private String sexx ; public String getSexx() { return sexx; } public void setSexx(String sexx) { this.sexx = sexx; } public void hello2( ) { System.out.println(sexx); } } public class Test { public static void main(String[] args) { Human a = new Human( ) ; a.setName("张三"); a.setAge(19); a.hello(); Man b = new Man( ) ; b.setName("李四"); b.setAge(34); b.setSex("我爱跑步"); b.hello(); b.hello1(); Woman c = new Woman( ) ; c.setAge(22); c.setName("王美丽"); c.setSexx("我爱听音乐"); c.hello(); c.hello2(); Human d =new Man() ; d.setAge(8); d.setName("小明" ) ; d.hello( ); } }
package 课后练习; //接口 public interface ShapePara { public static final double PI = 3.14 ; public double getArea( ) ; public double getCircumference( ) ; } //创建类 class Circle implements ShapePara { //成员属性 public double radius ; //圆的半径 private double x ;//圆心横坐标 protected double y ;//圆心纵坐标 //有参构造方法 Circle(double radius) { this.radius = radius ; x = 0 ; y = 0 ; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } //成员方法 public void setCenter(double x, double y) { this.x = x ; this.y = y ; } @Override public double getCircumference() { return 2*PI *radius; } @Override public double getArea() { return radius*radius*PI; } } //创建类 abstract class Shape2 { public abstract double getArea( ) ; } //子类 class Rect extends Shape2 implements ShapePara { //成员属性 private double a ; private double b ; public double getWeight() { return a; } public void setWeight(double weight) { this.a = weight; } public double getHeight() { return b; } public void setHeight(double height) { this.b = height; } @Override public double getArea() { return a*b ; } @Override public double getCircumference() { return (a+b)*2; } }
package 课后练习; public class Ceshitest { public static void main(String[] args) { Circle a = new Circle( 3 ) ; ShapePara s = a ; System.out.println("圆的周长"+s.getCircumference()); System.out.println("圆的面积"+s.getArea()); Rect b = new Rect( ) ; b.setHeight(2); b.setWeight(4); ShapePara c = b ; System.out.println("矩形面积"+c.getArea()); System.out.println("矩形周长"+c.getCircumference()); } }