Java学习笔记5

 
======================================================================
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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

炼丹狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值