1.定义类Human,具有若干属性和功能;定义其子类Man、Woman;在主类Test中分别创建子类、父类和上转型对象,并测试其特性。
public class Human { public String name; public int age; public String sex; 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public void name(String name) { System.out.println("姓名:"+name); } public void age(int age) { System.out.println("年龄"+age); } public void sex(String sex) { System.out.println("性别"+sex); } public void work() { System.out.println("11"); } }
//子类 public class Man extends Human { public void work() { System.out.println("家庭支柱"); } }
public class Woman extends Human { public void work() { System.out.println("家庭煮妇"); } }
public class Test { public static void main(String[] args) { // TODO 自动生成的方法存根 Human h = new Human(); h.age(20); Man m = new Man(); m.name("张子"); m.work(); Woman w = new Woman(); w.sex("女"); //隐式转换,向上转换,转换为父类,隐藏子类自身的属性 Human u = new Man(); u.sex("男"); //重写父类的方法,显示是子类的方法 Human a = new Woman(); a.work(); } }
2.编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。
public class Animal { private String kind; public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } public void eat() { System.out.println("吃饭饭咯"); } public void sheep() { System.out.println("数星星"); } }
public class Dog extends Animal { public void eat() { System.out.println("我吃骨头"); } public void sheep() { System.out.println("我趴着睡"); } }
public class Fish extends Animal { public void eat() { System.out.println("人家吃鱼吃的东西啦"); } public void sheep() { System.out.println("人家睡觉不闭眼啦"); } }
public class E2 { public static void main(String[] args) { // TODO 自动生成的方法存根 //实例父类,并调eat方法 Animal a = new Animal(); a.eat(); //向上转换,调子类的sheep方法 Animal aa = new Dog(); aa.sheep(); //调子类的eat方法 Animal n = new Fish(); n.eat(); } }
3.按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
public class Rect { protected double width; protected double lengt; public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLengt() { return lengt; } public void setLengt(double lengt) { this.lengt = lengt; } public double area() { return width*lengt; } }
public class Rectt extends Rect { private double height; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double tiji() { return lengt*width*height; } public double biaomainji() { return (lengt*width+lengt*height+width*height)*2; } }
public class TestRectt { public static void main(String[] args) { // TODO 自动生成的方法存根 Rect r =new Rect(); //矩形的长 r.getLengt(); r.setLengt(20); //矩形的宽 r.getWidth(); r.setWidth(30); System.out.println("矩形的面积"+r.area()); //向上转型 Rect rr = new Rectt(); //向下转型 Rectt r2= (Rectt)rr; //获取长方体的高 r2.getHeight(); r2.setHeight(30); //获取长方体的长 r2.getLengt(); r2.setLengt(20); //获取长方体的宽 r2.getWidth(); r2.setWidth(30); System.out.println("长方体的体积:"+r2.tiji()); System.out.println("长方体的表面积:"+r2.biaomainji()); }
4.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。
public class Vehicle { protected int wheels; protected 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 wheels() { System.out.println("我是父类,我无敌,车轮数,你猜"); } }
public class Car extends Vehicle { private int loader; public int getLoader() { return loader; } public void setLoader(int loader) { this.loader = loader; } public Car() { } public void weight() { System.out.println("我是子类,不好意思,我没有继承载重这个属性"); } public void loader() { System.out.println("这是我专有属性,我能载人啦。。。"); } }
public class Truck extends Car { private double payload; public Truck() { } public double getPayload() { return payload; } public void setPayload(double payload) { this.payload = payload; } public void payload() { System.out.println("我是孙子类,我载重很牛"); } }
public class Testche { public static void main(String[] args) { // TODO 自动生成的方法存根 Vehicle v = new Vehicle(); v.wheels(); //向上转型 Vehicle v2 = new Car(); //由Vehicle向下转型转至子类 Car c = (Car)v2; c.weight(); c.loader(); //向上转型 Vehicle v3 = new Truck(); //由Vehicle向下转型,转到孙子类 Truck t = (Truck)v3; t.payload(); } }
5.编写一个Shape类,具有属性:周长和面积;
定义其子类三角形和矩形,分别具有求周长的方法。
定义主类E,在其main方法中创建三角形和矩形类的对象,
并赋给Shape类的对象a、b,使用对象a、b来测试其特性。
public class Shape { private int zhouchang; private int area; public int getZhouchang() { return zhouchang; } public void setZhouchang(int zhouchang) { this.zhouchang = zhouchang; } public int getArea() { return area; } public void setArea(int area) { this.area = area; } }
public class Sanjiaoxing extends Shape { private int a; private int b; private int c; public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getC() { return c; } public void setC(int c) { this.c = c; } public int zhouchang() { return a+b+c; } }
public class Juxing extends Shape{ private int a; private int b; public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int zhouchang() { return (a+b)*2; } }
public class Test01 { public static void main(String[] args) { // TODO 自动生成的方法存根 //向上转型 Shape a =new Sanjiaoxing(); Shape b =new Juxing(); //向下转型 Sanjiaoxing aa=(Sanjiaoxing )a; aa.setA(3); aa.setB(4); aa.setC(3); System.out.println("三角形的周长:"+aa.zhouchang()); //向下转型 Juxing bb= (Juxing)b; bb.setA(4); bb.setB(5); System.out.println("矩形的周长:"+bb.zhouchang()); }