美文网首页
第七章:多形性

第七章:多形性

作者: pgydbh | 来源:发表于2018-04-18 11:23 被阅读13次

前言

这一章内容很多 我从中取出三点

目录

1.再谈继承
2.看看接口
3.static内部类&普通内部类

一:再谈继承

1.两种继承

①:普通继承(这里不再说)
举例:你家的青蛙,你给编号为0,1, 2, 3,4; 现在有一个类:青蛙 你想建立一个类表示你家的青蛙,这时候就是普通继承,因为青蛙也要能实例化,你家的青蛙又不全是普通青蛙的属性,多出了一个编号 int 。
重点:父类本身就可以实例化并需要存在,但是你觉得不能完全表达你的意思。
扩展:思考关于Gui编程中,继承窗口组件。
②:抽象继承(主要提到)
举例:形状是一个类,但是每种形状是不需要实例化的,因为实例化了它,没有任何意义。所以它只是一个模板,真正需要的是三角形,四边形,等等。
重点:父类本身不必实例化,父类只需要为子类提供一个填充模板。
扩展:联想list 与 arrayList & linkedList
③:注意
注意:虽然经常提到,但是继承的使用需要谨慎。继承使用的很少!
看法:自己认为的缺点,就是滥用继承,导致代码混乱。结构混乱。

2.代码演示

父类shape
package zero;

public abstract class Shape{
    protected int x;
    protected int y;

    public Shape(int x, int y){
        this.x = x;
        this.y = y;
    }

    public abstract void draw();
}
子类 Square
package zero;

public class Square extends Shape{

    private int width;
    private int height;
    public Square(int x, int y, int width, int height) {
        super(x, y);
        this.width = width;
        this.height = height;
    }

    @Override
    public void draw() {
        System.out.println("我是四边形,其实我不能画:\n" + "我的x:" + x + "   我的y:" + y
                            + "\n我的宽度=" +width + "   我的高度=" + height);
    }
}
子类 Line
package zero;

public class Line extends Shape{

    private int width;
    public Line(int x, int y, int width) {
        super(x, y);
        this.width = width;
    }

    @Override
    public void draw() {
        System.out.println("我是直线,其实我不能画:\n" + "我的x:" + x + "   我的y:" + y
                            + "\n我的长度=" +width);
    }
}
代码调用
public static void main(String[] args) {
    Shape shape = new Square(0, 0, 100, 100);
    shape.draw();

    System.out.println();
    Shape shape1 = new Line(0, 0, 100);
    shape1.draw();
}
输出
我是四边形,其实我不能画:
我的x:0   我的y:0
我的宽度=100   我的高度=100

我是直线,其实我不能画:
我的x:0   我的y:0
我的长度=100

Process finished with exit code 0

二:看看接口

区别:自己认为抽象类比较重量级,并且一个类只能继承一个类,接口比较轻量级,可以实现多个接口,接口默认public,抽象类可以有protected

1.实例1

interface
package zero;

public interface TestInterface {
    void say(String s);
}
实现接口
package zero;

public class Line implements TestInterface{

    @Override
    public void say(String s) {
        System.out.println(s);
    }
}
演示代码
public static void main(String[] args) {
        Line line = new Line();
        line.say("interface");
}

2.实例2(常用)

interface
package zero;

public interface TestInterface {
    void say(String s);
}
实现接口
package zero;

public class Line implements TestInterface{

    @Override
    public void say(String s) {
        System.out.println(s);
    }
}
调用接口
package zero;

public class Square{
    private TestInterface testInterface;
    public Square(TestInterface testInterface){
        this.testInterface = testInterface;
    }

    public void say(){
        testInterface.say("测试会不会改变");
    }
}
测试代码和输出
public static void main(String[] args) {
        Square square = new Square(new Line());
        square.say();
}

output:
测试会不会改变  你觉得呢

Process finished with exit code 0

三:static内部类&普通内部类

引用自:https://zhidao.baidu.com/question/149873207.html

1.静态内部类也叫嵌套类,用这个名字给他定义是更加形象的。意思是说内部类和外部类的关系只是层次嵌套关系,所以只是在创建类文件的时候类文件名是如下形式:outer$inner.java,在使用方面完全和两个普通类一样。

2.一般内部类在我看来才是真正的内部类,他们不仅有着嵌套关系,更重要的是内部类可以获得外部类的引用从而实现回调。而且通过创建内部类还可已让java实现真正的多继承!(interface名叫接口,顾名思义,他就是实现接口的,让一个类继承多个接口实现多继承是不合适的)

解读1(static 内部类):只是层次上的嵌套关系,使用时如果两个普通类。(淡然这个不足以应对面试,但是我觉得很有道理)
解读2 (普通内部类):这样的内部类可以获得外部类的引用,实现操作。

相关文章

  • 第七章:多形性

    前言 这一章内容很多 我从中取出三点 目录 1.再谈继承 2.看看接口 3.static内部类&普通内部类 一:再...

  • Java多形性

    封装 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构同时也保护了数据。对外界而已它的内部细节...

  • 癌在多形性腺瘤中

    (carcinoma ex pleomorphic adenoma)(图5.32~5.33) 定义 癌在多形性腺瘤...

  • 小考点

    牙源性腺样瘤-----------------玫瑰花样结构 涎腺多形性腺瘤--------------双层导管结构...

  • 2018-03-31(接口、组织、继承、多形性)

    接口: 规定了可对一个特定的对象发出哪些请求。然而,必须在某个地方存在着一些代码,以便满足这些请求。这些代码隐藏起...

  • 抗癌新突破!抗疟药物可能有效抵抗致命癌症,延长患者的生存期

    多形性胶质母细胞瘤(GBM)是一种侵袭性脑肿瘤,具有典型的致死性。弗吉尼亚联邦大学(VCU)Massey癌症中心和...

  • 2018-10-16 新闻百科:爱美女生为何连连叫苦?_湿疹症状

    大新闻:爱美女生为何连连叫苦?_湿疹症状有那些?_湿疹主要症状表现? 湿疹的症状很多:湿疹症状表现为多形性密集的粟...

  • 磨砺前行

    上周因为药物过敏,被诊断为“多形性红斑”。 住院的一个星期里,感触良多,重新审视了我的生活。 原本以为,住院每天的...

  • 不动杆菌属

    为革兰阴性球状或球杆菌,呈多形性,成对排列或者呈短链。革兰染色不易脱色,需注意鉴别。 条件致病菌,属奈瑟菌属,适宜...

  • 湿疹、荨麻疹诊断及用药

    湿疹、荨麻疹是皮肤病中的多发病、常见病。其发病形态为多形性、弥漫性红斑丘疹,分布对称,急性者有渗出,慢性...

网友评论

      本文标题:第七章:多形性

      本文链接:https://www.haomeiwen.com/subject/ekfvkftx.html