Java学习笔记5

本文探讨了Java中多态的应用方式,包括在返回值和参数中的使用,并对比了继承与组合两种代码复用方法的特点及适用场景。
 
======================================================================
java 第五天
======================================================================
(1)
不要父类的某些方法.
继承复用: //不筛选的用继承关系把父类的所有代码完全的继承过来。
class Liucy {
       void techcpp() {
              System.out.println("TechC++");
       }
}
class Huxz extends Liucy {
}
组合复用:  // 设计原则: 组合优于继承,在代码复用的时候.
class Huxz1 {
       Liucy liucy = new Liucy();
       publicvoid techCpp() {
              liucy.techcpp(); // 方法的扩散.一个类调用另一个类的方法,而这个类又调用另一个类的方法.
       }
}
(2)
多态的应用.
多态用在返回值; //理解不深刻,看看老师的代码:
----------------------------------------------------------------------------------------------------------------------
package test;
publicclass TestShape {
       publicstaticvoid 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 {
       publicdouble line() {
              return 0;
       }
       publicdouble area() {
              return 0;
       }
}
class Rect extends Shape {
       privatedoublechang;
       privatedoublekuan;
       public Rect() {
       }
       public Rect(double chang, double kuan) {
              this.chang = chang;
              this.kuan = kuan;
       }
       publicdouble line() {
              return (chang + kuan) * 2;
       }
       publicdouble area() {
              returnchang * kuan;
       }
}
class Square extends Rect {
       privatedoublebianChang;
       public Square() {
       }
       public Square(double bianChang) {
              this.bianChang = bianChang;
       }
       publicdouble line() {
              returnbianChang * 4;
       }
       publicdouble area() {
              returnthis.bianChang * this.bianChang;
       }
}
class Circle extends Shape {
       privatedoubler;
       public Circle(double r) {
              this.r = r;
       }
       publicdouble line() {
              return 2 * r * Math.PI;
       }
       publicdouble area() {
              returnr * r * Math.PI;
       }
}
-----------------------------------------------------
多态用在参数上;               
package test;
publicclass TestPoly {
       publicstaticvoid 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);
       }
       publicstatic Animal1 getAnimal(int type) {
              if (type == 0)
                     returnnew Dog();
              else
                     returnnew Cat();
       }
       publicstaticvoid feed(Animal1 a) {
              a.eat();
       }
}
class Animal {
       publicvoid eat() {
       }
}
class Dog extends Animal1 {
       publicvoid eat() {
              System.out.println("Dog Eat Bone");
       }
}
class Cat extends Animal1 {
       publicvoid eat() {
              System.out.println("Cat Eat Fish");
       }
}
修改实现,对架构的影响最小.单继承:
接口像一个类的额外的信息().
java的多继承是有主次的,有主类型,附加的类型(副类型[接口]是一个附加信息.)
java中的多继承: 区分主类型和次要类型,接口的引用就使得我们可以对事物的共性,作一个再抽象.抽象出副类型.这种多继承的关系,是不会破坏单继承树状关系的复杂度.
 
 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

炼丹狮

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

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

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

打赏作者

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

抵扣说明:

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

余额充值