//需求1:我们有多个图形的对象,有矩形,圆形,三角形,需要统一管理它们,并且按照面积排序。 public class TestUse { public static void main(String[] args) { //声明图形数组 Graphic[] arr = new Graphic[3];//数组的元素类型声明为父类类型 arr[0] = new Rectangle(3,2); //左边的arr[0]是Graphic类型,右边的对象是Rectangle对象,是多态引用 arr[1] = new Circle(1.1); arr[2] = new Triangle(3,4,5); //遍历输出三个图形的信息 for (int i=0; i<arr.length; i++){ // System.out.println(arr[i].getLength());//错误,arr[i]此时编译时类型是Graphic,无法访问子类Rectangle的getLength() System.out.println("第" + (i+1) +"个图形的面积是:" + arr[i].area()); } //冒泡排序,从小到大 for(int i=1; i<arr.length; i++){ for(int j=0; j<arr.length-i; j++){ if(arr[j].area() > arr[j+1].area()){ //交换arr[j]与arr[j+1]的对象 Graphic temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } //排序后显示结果 System.out.println("排序后:"); for (int i = 0; i < arr.length; i++) { System.out.println("第" + (i+1) +"个图形的面积是:" + arr[i].area()); } } } //声明所有图形的公共父类,来体现所有图形的共同特征,比如:求面积功能 class Graphic{ double area(){ return 0.0; } } class Rectangle extends Graphic{ private double length; private double width; public Rectangle() { } public Rectangle(double length, double width) { this.length = length; this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } //重写方法快捷键,Ctrl + O @Override //可以去掉,它是一个注解,表示下面的方法是一个重写的方法 public double area() { return length * width; } } class Circle extends Graphic{ private double radius; public Circle() { } public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } //重写方法快捷键,Ctrl + O @Override double area() { return Math.PI * radius * radius; } } class Triangle extends Graphic{ private double a; private double b; private double c; public Triangle() { } public Triangle(double a, double b, double c) { this.a = a; this.b = b; this.c = 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; } //重写方法快捷键,Ctrl + O @Override double area() { //三角形的三条边是有要求的 if(a+b>c && a+c>b && b+c>a && a>0 && b>0 && c>0){//任意两边之后大于第三边,才是合格的三角形,才需要求面积 //求面积,海伦公式,根据3条边求面积 double p = (a+b+c)/2; return Math.sqrt(p*(p-a)*(p-b)*(p-c)); } return 0.0; } }
讯享网
讯享网//需求2:我们有不同的动物对象,我们测试类提供一个方法,可以观察不同动物对象的eat行为 public class TestUse2 { public static void main(String[] args) { Dog a1 = new Dog(); lookAnimalEat(a1);//方法调用时,实参a1给形参animal赋值,相当于 Animal animal = a1; 左边是父类类想,右边是子类对象 Cat a2 = new Cat(); lookAnimalEat(a2); } public static void lookAnimalEat(Animal animal){ animal.eat(); } //方法的重载 /* public static void lookAnimalEat(Dog animal){ animal.eat(); } public static void lookAnimalEat(Cat animal){ animal.eat(); }*/ } class Animal{ public void eat(){ System.out.println("亚米亚米亚米"); } } class Dog extends Animal{ @Override public void eat() { System.out.println("啃骨头"); } } class Cat extends Animal{ @Override public void eat() { System.out.println("吃鱼"); } }
//需求3:我们有不同种类的汽车,根据用户的需求不同,可以生产不同的汽车 public class TestUse3 { public static void main(String[] args) { Car car1 = Factory.createCar(15);//左边car1是父类类型,实际返回的是的子类对象 car1.drive(); Car car2 = Factory.createCar(50);//左边car2是父类类型,实际返回的是BMW的子类对象 car2.drive(); } } class Factory{ public static Car createCar(double money){ if(money < 20){ return new (); }else{ return new BMW(); } } } class Car{ public void drive(){ System.out.println("滴滴滴..."); } } class BMW extends Car{ @Override public void drive() { System.out.println("坐在宝马车里就是开心"); } } class extends Car{ @Override public void drive() { System.out.println("坐在车里面有点不开心"); } }
讯享网//需求4:表示某个人的信息,包含这个人的名字,还有它伴侣的对象,并且创建对象,测试 public class TestUse4 { public static void main(String[] args) { Human human = new Human(); human.name = "Jane"; Orangutan orangutan = new Orangutan(); orangutan.name = "金刚"; human.fere = orangutan;//fere声明的类型是Creature,赋值的是Orangutan子类的对象 System.out.println("human的名字是" + human.name + ",他伴侣的名字:" + human.fere.name); Human human2 = new Human(); human2.name = "董奕辰"; Human girl = new Human(); girl.name = "如花"; human2.fere = girl;//fere声明的类型是Creature,赋值的是Human子类的对象 System.out.println("human2的名字是" + human2.name + ",他伴侣的名字:" + human2.fere.name); } } //Creature:生物 class Creature{ String name; } class Human extends Creature{ Creature fere; } //Orangutan:猩猩 class Orangutan extends Creature{ }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/90552.html