Java学习笔记5

    技术2022-05-11  42

    ====================================================================== java 第五天 ======================================================================(1) 不要父类的某些方法. 继承复用 : // 不筛选的用继承关系把父类的所有代码完全的继承过来。 class Liucy {        void techcpp() {               System. out .println( "TechC++" );        } } class Huxz extends Liucy { } 组合复用 :  // 设计原则 : 组合 优于 继承 , 在代码复用的时候 . class Huxz1 {        Liucy liucy = new Liucy();        public void techCpp() {               liucy.techcpp(); // 方法的扩散 . 一个类调用另一个类的方法 , 而这个类又调用另一个类的方法 .        } } (2) 多态的应用. 多态用在返回值上 ; // 理解不深刻 , 看看老师的代码 :---------------------------------------------------------------------------------------------------------------------- package test; public class TestShape {        public static void main(String[] args) {               Shape[] s = new Shape[3];               s[0] = new Rect(5, 3);               s[1] = new Square(4);               s[2] = new Circle(10);               for ( int i = 0; i < s. length ; i++) {                      System. out .println(s[i].line());                      System. out .println(s[i].area());               }        } } class Shape {        public double line() {               return 0;        }        public double area() {               return 0;        } } class Rect extends Shape {        private double chang ;        private double kuan ;        public Rect() {        }        public Rect( double chang, double kuan) {               this . chang = chang;               this . kuan = kuan;        }        public double line() {               return ( chang + kuan ) * 2;        }        public double area() {               return chang * kuan ;        } } class Square extends Rect {        private double bianChang ;        public Square() {        }        public Square( double bianChang) {               this . bianChang = bianChang;        }        public double line() {               return bianChang * 4;        }        public double area() {               return this . bianChang * this . bianChang ;        } } class Circle extends Shape {        private double r ;        public Circle( double r) {               this . r = r;        }        public double line() {               return 2 * r * Math. PI ;        }        public double area() {               return r * r * Math. PI ;        } } ----------------------------------------------------- 多态用在参数上;                package test; public class TestPoly {        public static void main(String[] args) {               /*                * int type=Integer.parseInt(args[0]); Animal a=getAnimal(type);                * a.eat();                */               Dog d = new Dog();               Cat c = new Cat();               feed(d); // 传入不同的对象 .               feed(c);        }        public static Animal1 getAnimal( int type) {               if (type == 0)                      return new Dog();               else                      return new Cat();        }        public static void feed(Animal1 a) {               a.eat();        } } class Animal {        public void eat() {        } } class Dog extends Animal1 {        public void eat() {               System. out .println( "Dog Eat Bone" );        } } class Cat extends Animal1 {        public void eat() {               System. out .println( "Cat Eat Fish" );        } } 修改实现,对架构的影响最小.单继承: 接口像一个类的额外的信息().java的多继承是有主次的,有主类型,附加的类型(副类型[接口]是一个附加信息.)java中的多继承: 区分主类型和次要类型,接口的引用就使得我们可以对事物的共性,作一个再抽象.抽象出副类型.这种多继承的关系,是不会破坏单继承树状关系的复杂度.    

    最新回复(0)