温馨提示:

本篇幅较长,非战斗人员请撤退。。。


一、包

包是什么?

包的本质

包的本质 实际上就是创建不同的文件夹来保存类文件

java常用的包

包的使用

二、访问修饰符

访问修饰符是什么?

访问范围(重点)

三、封装

封装是什么?

封装就是把抽象的属性和方法封装在一起,数据被保护在内部,程序的其他部分只能通过被授权的方法才能访问。

封装的好处:

1)隐藏实现细节

2)可以对数据进行验证,保证合理安全

封装步骤

四、继承

继承是是什么?

继承的使用

1)继承使代码的复用性提高了

2)代码的可扩张性和可维护性提高了

使用细节:

1)子类继承了所有的属性和方法,但是私有属性和方法不能在子类中直接方法,要通过父类提供的公共方法访问

2)子类必须调用父类的构造器完成父类的初始化

3)当创建子类对象时,不管使用子类的哪个构造器,默认情况下总会调用父类的无参构造器,如果父类没有提供无参构造器,则必须在子类的构造器中用super去指定使用父类的哪个构造完成对父类的初始化工作,否则编译不通过。

4)如果希望指定调用父类的哪个构造器,则显式的调用一下

5)super在使用时需放在构造器的第一行

6)super()和this()都只能放在构造器的第一行,因此这两个方法不能共存在一个构造器

7)java中所有类都是Object类的子类

8)父类构造器的调用不限于直接父类,将一直往上追溯到顶级父类(Object 类)

9)子类最多只能继承一个父类

10)不能滥用继承,必须满足is-a的关系

如:Person is a Music ×

Cat is a Anaimal √

继承的本质

练习一:

/*** @author: 程序员飞扬* @description:*/
public class A {A(){System.out.println("a");}A(String name){System.out.println("a+name");}
}
/*** @author: 程序员飞扬* @description:*/
public class B extends A{B(){this("abc");System.out.println("b");}B(String name){System.out.println("b+name");}public static void main(String[] args) {B b = new B();}
}

输出:

a

b+name

b

五、super关键字

1.super关键字是什么?

super代表父类的引用,用于访问父类的属性,方法和构造器

2.基本语法

使用

super和this的比较

六、方法的重写

重写是什么?

子类拥有和父类一样(返回值类型,方法名,参数)的方法称为重写。

方法重写细节

七、多态

方法和对象具有多种形态是面向对象的第三大特征,多态是建立在封装和继承的基础之上的。

方法的多态

重载和重写就体现了方法的多态

对象的多态(重点,难点)

1)对象多态案例:

/*** @author: 程序员飞扬* @description:主人类*/
public class Master {private String name;public Master(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void feed(Animal animal,Food food){System.out.println("主人" + name + "给" + animal.getName() + "喂食" + food.getName());}
}
/*** @author: 程序员飞扬* @description:动物父类*/
public class Animal {private String name;public Animal(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
/*** @author: 程序员飞扬* @description:狗子类*/
public class Dog extends Animal{public Dog(String name) {super(name);}
}
/*** @author: 程序员飞扬* @description:猫子类*/
public class Cat extends Animal{public Cat(String name) {super(name);}
}
/*** @author: 程序员飞扬* @description:食物父类*/
public class Food {private String name;public Food(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
/*** @author: 程序员飞扬* @description:鱼子类*/
public class Fish extends Food{public Fish(String name) {super(name);}
}
/*** @author: 程序员飞扬* @description:骨头子类*/
public class Bone extends Food{public Bone(String name) {super(name);}
}

测试:

/*** @author: 程序员飞扬* @description:对象多态测试*/
public class Poly01 {public static void main(String[] args) {Master master = new Master("小王");Animal animal = new Dog("二哈");Food food = new Bone("骨头");master.feed(animal,food);Master master2 = new Master("小王");Animal animal2 = new Cat("喵喵");Food food2 = new Bone("黄花鱼");master2.feed(animal2,food2);}
}

输出:

主人小王给二哈喂食骨头

主人小王给喵喵喂食黄花鱼

2)多态的使用注意事项

向上转型:

/*** @author: 程序员飞扬* @description:动物父类*/
public class Animal {String name="动物";int age = 10;public void eat(){System.out.println("吃");}
}
/*** @author: 程序员飞扬* @description:猫子类*/
public class Cat extends Animal{public void eat(){System.out.println("猫吃鱼");}public void catchMouse(){System.out.println("猫抓老鼠");}
}
/*** @author: 程序员飞扬* @description:多态注意事项:向上转型*/
public class PolyDetail {public static void main(String[] args) {//向上转型Animal animal = new Cat();animal.eat();System.out.println("ok...");}
}

输出:

猫吃鱼

ok...

向下转型:

/*** @author: 程序员飞扬* @description:多态注意事项:向下转型*/
public class PolyDetail {public static void main(String[] args) {//向下转型Animal animal = new Cat();Cat cat = (Cat)animal;cat.catchMouse();//Dog dog = (Dog)animal;//运行时会抛异常:ClassCastException}
}

输出:

猫抓老鼠

属性重写

多态练习一:

多态练习二:

/*** @author: 程序员飞扬* @description:*/
public class Base {int count = 20;public void display(){System.out.println(this.count);}
}
/*** @author: 程序员飞扬* @description:*/
public class Sub extends Base{int count = 10;public void display(){System.out.println(this.count);}
}
/*** @author: 程序员飞扬* @description:*/
public class PolyTest2 {public static void main(String[] args) {Sub s = new Sub();System.out.println(s.count);//10s.display();//10Base b = s;System.out.println(b == s);//trueSystem.out.println(b.count);//20b.display();//10}
}

动态绑定机制

多态数组

应用案例:

多态数组一:

/*** @author: 程序员飞扬* @description:*/
public class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String say(){return name + "\t" +age;}
}
/*** @author: 程序员飞扬* @description:*/
public class Teacher extends Person{private double salay;public Teacher(String name, int age,double salay) {super(name, age);this.salay = salay;}public double getSalay() {return salay;}public void setSalay(double salay) {this.salay = salay;}@Overridepublic String say() {return super.say() + "\t" + "salay:" + salay;}
}
/*** @author: 程序员飞扬* @description:*/
public class Student extends Person{private double score;public Student(String name, int age,double score) {super(name, age);this.score = score;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}@Overridepublic String say() {return super.say() + "\t" + "score:" + score;}
}
/*** @author: 程序员飞扬* @description:动态数组测试*/
public class PolyArray {public static void main(String[] args) {Person[] person = new Person[3];person[0] = new Person("Jone",99);person[1] = new Teacher("Smith",35,20000);person[2] = new Student("Jack",18,98);for (int i = 0; i < person.length; i++) {String say = person[i].say();//动态绑定System.out.println(say);}}
}

输出

Jone 99

Smith 35 salay:20000.0

Jack 18 score:98.0

多态数组二:

/*** @author: 程序员飞扬* @description:*/
public class Teacher extends Person{private double salay;public Teacher(String name, int age,double salay) {super(name, age);this.salay = salay;}public double getSalay() {return salay;}public void setSalay(double salay) {this.salay = salay;}@Overridepublic String say() {return super.say() + "\t" + "salay:" + salay;}public void teach(){System.out.println("老师" + getName() + "正在授课");}
}
/*** @author: 程序员飞扬* @description:*/
public class Student extends Person{private double score;public Student(String name, int age,double score) {super(name, age);this.score = score;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}@Overridepublic String say() {return super.say() + "\t" + "score:" + score;}public void study(){System.out.println("学生" + getName() + "正在学习");}
}
/*** @author: 程序员飞扬* @description:动态数组升级测试*/
public class PolyArray {public static void main(String[] args) {Person[] person = new Person[3];person[0] = new Person("Jone",99);person[1] = new Teacher("Smith",35,20000);person[2] = new Student("Jack",18,98);/*for (int i = 0; i < person.length; i++) {String say = person[i].say();//动态绑定System.out.println(say);}*/for (int i = 0; i < person.length; i++) {String say = person[i].say();//动态绑定System.out.println(say);if(person[i] instanceof Teacher){Teacher teacher = (Teacher) person[i];teacher.teach();}else if(person[i] instanceof Student){Student student = (Student)person[i];student.study();}else{}}}
}

输出:

Jone 99

Smith 35 salay:20000.0

老师Smith正在授课

Jack 18 score:98.0

学生Jack正在学习

多态参数

应用案例:

代码示例

/*** @author: 程序员飞扬* @description:*/
public class Employee {private String name;private double salary;public Employee(String name, double salary) {this.name = name;this.salary = salary;}public Double getAnnual(){return this.salary * 12;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}
}
/*** @author: 程序员飞扬* @description:*/
public class Worker extends Employee{public Worker(String name, double salary) {super(name, salary);}public void work(){System.out.println("员工" + getName() + "在工作。。。");}@Overridepublic Double getAnnual() {return super.getAnnual();}
}
/*** @author: 程序员飞扬* @description:*/
public class Manager extends Employee{private Double bonus;public Manager(String name, double salary,double bonus) {super(name, salary);this.bonus = bonus;}public void manage(){System.out.println("经理"+ getName() + "在管理。。。");}@Overridepublic Double getAnnual() {return super.getAnnual() + bonus;}
}
/*** @author: 程序员飞扬* @description:*/
public class PolyParameterTest {public static void main(String[] args) {Employee employee = new Employee("张三", 12000);Worker worker = new Worker("小王",10000);Manager manager = new Manager("老李",12000,50000);PolyParameterTest polyParameterTest = new PolyParameterTest();polyParameterTest.showEmpAnnal(worker);polyParameterTest.showEmpAnnal(manager);polyParameterTest.testWork(worker);polyParameterTest.testWork(manager);}public void showEmpAnnal(Employee e){System.out.println(e.getAnnual());}public void testWork(Employee e){if(e instanceof Worker){((Worker) e).work();}else if(e instanceof Manager){((Manager) e).manage();}}
}

输出:

120000.0

194000.0

员工小王在工作。。。

经理老李在管理。。。


Java筑基10-封装继承多态(重点)相关推荐

  1. python2筑基-(封装/继承/多态/类属性)

    继承 单继承 """""" """ 在对象中,有种关系叫做父子关系 继承--子类拥有父类中定义的属性,函数语法 ...

  2. 大数据笔记8—java基础篇4(面向对象-封装-继承-多态)

    面向对象 一.面向对象 1.面向过程 1.2.举例 1.3.总结 二.面向对象 1.简述 2.举例 3.思想特点 2.1.类的定义格式 2.1.1.简述 2.2.2.格式 2.3.3.示例 三.类的使 ...

  3. Java继承_Hachi君浅聊Java三大特性之 封装 继承 多态

    Hello,大家好~我是你们的Hachi君,一个来自某学院的资深java小白.最近利用暑假的时间,修得满腔java语言学习心得.今天小宇宙终于要爆发了,决定在知乎上来一场根本停不下来的Hachi君个人 ...

  4. Day55-每日一道Java面试题-Java 面向对象编程三大特性: 封装 继承 多态

    Java 面向对象编程三大特性: 封装 继承 多态 封装 封装把一个对象的属性私有化,同时提供一些可以被外界访问的属性的方法,如果属性不想被外界访问,我们大可不必提供方法给外界访问.但是如果一个类没有 ...

  5. python 参数类型的多态_【Python】面向对象:类与对象\封装\继承\多态

    六.Python面向对象--类与对象\封装\继承\多态 1.什么是面向对象编程 1.1 程序设计的范式:程序可控,易于理解 1.2 抽象并建立对象模型 1.3 程序是不同对象相互调用的逻辑.每个对象在 ...

  6. c语言编程 菲薄拉,C语言设计模式-封装-继承-多态

    快过年了,手头的工作慢慢也就少了,所以,研究技术的时间就多了很多时间,前些天在CSDN一博客看到有大牛在讨论C的设计模式,正好看到了,我也有兴趣转发,修改,研究一下. 记得读大学的时候,老师就告诉我们 ...

  7. 小白理解——封装继承多态

                                      一.封装 是什么:首先是抽象,把事物抽象成一个类,其次才是封装.对外表示为一个对象,隐藏对象的属性和动作实现的细节,仅对外公开接口. ...

  8. 白话文带你了解 封装 继承 多态

    这里讲的仅仅是带你理解 封装 继承 多态 染好您可以移步去别的文章学习具体的实现 (只是个人理解 您可以不爱,请别伤害哦!) 首先你要知道 java是面向对象的,说白了就是我拿你当个人,请认清你的地位 ...

  9. python多态的三种表现形式_python小结----面向对象的三大特征(封装,继承,多态)

    面向对象的三大特征: 封装,继承,多态 面向对象的编程思想核心:高类聚,低耦合–程序的设计模式范畴 封装 什么是封装: 在面向对象编程的思想中,对代码进行高度封装,封装又叫包装 封装就是指将数据或者函 ...

  10. python--编写程序:实现乐手弹奏乐器,乐手可以弹奏不同的乐器而发出不同的声音------使用类的封装继承多态的问题/使用面向对象的思想,设计自定义类,描述出租车和家用轿车的信息

    编写程序:实现乐手弹奏乐器,乐手可以弹奏不同的乐器而发出不同的声音 ------使用类的封装继承多态的问题 class Instrumnet():#乐器类def make_sound(self):pa ...

最新文章

  1. _BLOCK_TYPE_IS_VALID错误
  2. 请求https错误: unable to find valid certification
  3. 基于PCA的人脸特征抽取
  4. JEECG 3.7 新装亮相,移动APP发布
  5. Hyperledger Fabric 2.2.1 区块链问题汇总(持续更新)
  6. 线上MySQL某个历史数据表的分区笔记
  7. C# Aspose 去除水印 亲测有效!!!(有效测试时间:20220806)
  8. es高亮搜索java_ES检索服务搜索结果高亮
  9. emacs ido模式
  10. 性能优化的终极目标-内存简析
  11. MTK6577+Android之TP(触摸屏)
  12. drawio二次开发
  13. 02.使用fmod实现QQ变声效果
  14. 作业1.1利用Audacity软件分析音频
  15. UBR/CBR/VBR
  16. Pipeline 基础步骤
  17. 买笔记本要注意什么呢?
  18. 计算机上的计算器缺陷报告咋写,windows计算器测试报告–.doc
  19. 华中师范大学计算机学院杨青,杨青(实力派影视演员)_百度百科
  20. 光纤通信 波长 光谱展宽 脉冲宽度

热门文章

  1. 有一个3×4的矩阵,要求编程序求出其中值最大的那个元素的值,以及其所在的行号和列号
  2. 老式十字锁自动碰锁,换锁芯
  3. What The F**k Python!!!
  4. STM32时钟简介及系统时钟频率的更改方式
  5. 租用GPU服务器跑深度学习模型心得
  6. 伽卡他卡学生端找不到计算机,伽卡他卡电子教室
  7. xlsx表格怎么筛选重复数据_excel表格如何过滤筛选重复项内容
  8. Win7+Ubuntu16.04双系统安装方法
  9. 如何做投资--入门篇
  10. 2020牛客暑期多校训练营(第九场) Groundhog and 2-Power Representation