Java面向对象笔记

多态

一种类型的变量可以引用多种实际类型的对象

java 复制代码
package ooplearn;

public class Test {

    public static void main(String[] args) {
        Animal[] animals = new Animal[2];
        animals[0] = new Dog();
        animals[1] = new Cat();

        for (Animal animal : animals){
            animal.eat();
        }
    }
}

class Animal {
    public void eat() {
        System.out.println("Animal eat");
    }
}

class Dog extends Animal{
    public void eat(){
        System.out.println("Dog eat");
    }
}

class Cat extends Animal{
   public void eat(){
        System.out.println("Cat eat");
    }
}

Animal类型的变量animal可以引用Dog和Cat类型对象,称为多态 。Animal就是animal变量的静态类型 ,Dog和Cat就是animal变量的动态类型 。animal.eat()调用的是变量动态类型的方法,称为动态绑定

静态绑定

java 复制代码
package ooplearn;

public class Test {

    public static void main(String[] args) {
        Dog d = new Dog();
        Animal a = d;
        System.out.println(d.title); // return Dog
        System.out.println(a.title); // return Animal
    }
}
class Animal {
    public static String title = "Animal";
}

class Dog extends Animal{
    public static String title = "Dog";
}

变量d的静态类型是Dog,访问Dog变量和方法,变量a的静态类型是Animal,访问Animal的变量和方法。访问绑定到变量的静态类型,称静态绑定

实例方法调用顺序

java 复制代码
package ooplearn;

public class Test {

    public static void main(String[] args) {
        Animal[] animals = new Animal[2];
        animals[0] = new Dog();
        animals[1] = new Cat();

        for (Animal animal : animals){
            animal.eat();
        }
    }
}

class Animal {
    public void eat() {
        System.out.println("Animal eat");
    }
}

class Dog extends Animal{
    public void eat(){
        System.out.println("Dog eat");
    }
}
class Cat extends Animal{
}

上述代码返回

复制代码
Dog eat
Animal eat

循环输出的过程animal变量的实际类型分别为Dog和Cat,先从实际类型找方法,找不到就去父类找。

内部类

定义在类里的类称为内部类,一般与外部类(包含内部类的类)关系密切,与其他类关系不大。一般对外隐藏,有更好的封装性。如Swing编程中为组件创建ActionListener:

java 复制代码
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class TwoButtons {
    JFrame frame;
    JLabel label;

    public static void main(String[] args) {
        TwoButtons gui = new TwoButtons();
        gui.go();
    }

    public void go() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton labelButton = new JButton("Change Label");
        labelButton.addActionListener(new LabelListener());

        JButton colorButton = new JButton("Change Circle");
        colorButton.addActionListener(new ColorListener());

        label = new JLabel("I'm a label");
        MyDrawPanel drawPanel = new MyDrawPanel();
    }

    class LabelListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            label.setText("Ouch!");
        }
    }

    class ColorListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            frame.repaint();
        }

    }
}

权限修饰符

修饰 本类 本包 其他包子类 其他包
private Y N N N
不写 Y Y N N
protected Y Y Y N
public Y Y Y Y

private修饰部分只能在本类中使用;缺省状态下,可见性上升到当前包内,即在同一个包内的地方可以使用;protected范围还包括本包之外其他包属于自己子类的部分;public没限制。

导图

相关推荐
bxlj_jcj21 分钟前
JVM性能优化之年轻代参数设置
java·性能优化
八股文领域大手子21 分钟前
深入理解缓存淘汰策略:LRU 与 LFU 算法详解及 Java 实现
java·数据库·算法·缓存·mybatis·哈希算法
不当菜虚困35 分钟前
JAVA设计模式——(八)单例模式
java·单例模式·设计模式
m0_7401546742 分钟前
Maven概述
java·maven
无敌小茶1 小时前
Linux学习笔记之动静态库
linux·笔记
吗喽对你问好1 小时前
Java位运算符大全
java·开发语言·位运算
Java致死1 小时前
工厂设计模式
java·设计模式·简单工厂模式·工厂方法模式·抽象工厂模式
DXM05211 小时前
牟乃夏《ArcGIS Engine地理信息系统开发教程》学习笔记3-地图基本操作与实战案例
开发语言·笔记·学习·arcgis·c#·ae·arcgis engine
程序员JerrySUN2 小时前
驱动开发硬核特训 · Day 21(上篇) 抽象理解 Linux 子系统:内核工程师的视角
java·linux·驱动开发
只因只因爆2 小时前
如何在idea中写spark程序
java·spark·intellij-idea