当前位置: 首页 > news >正文

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()调用的是变量动态类型的方法,称为动态绑定

静态绑定

package ooplearn;public class Test {public static void main(String[] args) {Dog d = new Dog();Animal a = d;System.out.println(d.title); // return DogSystem.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的变量和方法。访问绑定到变量的静态类型,称静态绑定

实例方法调用顺序

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:


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 {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stublabel.setText("Ouch!");}}class ColorListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubframe.repaint();}}
}

权限修饰符

修饰本类本包其他包子类其他包
privateYNNN
不写YYNN
protectedYYYN
publicYYYY

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

导图

在这里插入图片描述

http://www.lryc.cn/news/363796.html

相关文章:

  • 如何通过PHP语言实现远程控制多路照明
  • Capture One Pro 23:专业 Raw 图像处理的卓越之选
  • 【主题广泛|投稿优惠】2024年交通运输与信息科学国际会议(ICTIS 2024)
  • 表格误删数据保存关闭后如何恢复?5个恢复方法大公开!
  • Go 语言中的切片:灵活的数据结构
  • 在鲲鹏服务器搭建k8s高可用集群分享
  • MySQL之数据库事务机制学习笔记(五)
  • linux 系统被异地登录,cpu占用拉满100%
  • 智慧校园应用平台的全面建设
  • 图论第6天
  • Redis教程(二十一):Redis怎么保证缓存一致性
  • android apk签名
  • flutter 解析json另类封装方式 List<bean>,哈哈哈
  • 哈希表(Hash table)
  • 【c语言】自定义类型-结构体
  • 2-链表-71-环形链表 II-LeetCode142
  • 【UnityShader入门精要学习笔记】第十七章 表面着色器
  • Python社会经济 | 怀特的异方差一致估计量
  • 《被讨厌的勇气》笔记
  • Python爬虫协程批量下载图片
  • Flask Web开发基础:数据库与ORM实战
  • pidstat -d 1分析磁盘吞吐量
  • 期望20K,2年golang深圳某互联网小公司一面
  • #02 安装指南:如何配置Stable Diffusion环境
  • 拼多多笔试
  • Golang | Leetcode Golang题解之第119题杨辉三角II
  • Flutter 中的 SliverIgnorePointer 小部件:全面指南
  • 比较两台计算机上的LabVIEW、工具包及驱动程序的一致性
  • 参考——温湿度传感器DHT11驱动_STM32
  • 架构每日一学 14:架构师如何进行可行性探索?