Java 面向对象各节
Java自学笔记——Java面向对象——01.回顾方法
Java自学笔记——Java面向对象——02.构造器、类和对象
Java自学笔记——Java面向对象——03.封装、继承、多态
Java自学笔记——Java面向对象——04.抽象类、接口、内部类
一、封装
- 提高程序的安全性,保护数据
- 隐藏代码的实现细节
- 统一接口
- 系统可维护性增加了
public class Student {
//属性私有
private String name; // 名字
private int id; //学号
private char sex; //性别
private int age;
//提供一些可以操作这个展性的方法!
//提供一些 public的 get、set方法
// get获得这个数据
public String getName(){
return this.name;
}
//set给这个数据设置值
public void setName(String name){
this.name = name;
}
// alt + inset
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age>120||age<0){
this.age = 3;
}else {
this.age = age;
}
}
}
调用上,运行下
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("sk");
System.out.println(s1.getName());
s1.setAge(999);
System.out.println(s1.getAge());
}
}
二、继承
- 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模。
- extends的意思是“扩展”。子类是父类的扩展
- JAVA中类只有单继承,没有多继承!
- 继承是类和类之间的一种关系。除此之外类和类之间的关系还有依赖、组合、聚合等。
- 继承关系的俩个类,一个为子类(派生类)一个为父类(基类)。子类继承父类使用关键字 extends来表示。
- 子类和父类之间从意义上讲应该具有"¨is a"的关系
- object类
- super
- 方法重写
Person.java
package com.oop.demo05;
// 在Java 中,所有的类,都默认直接或者间接继承 Object
// Person 人 : 父类
public class Person {
//public
// protected
//default
//private
private int money = 10_0000_0000;
public void say(){
System.out.println("说了一句话");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
Student.java
package com.oop.demo05;
// 继承
// 学生 is 人 : 派生类,子类
// 子类继了父类,就会加有父类的全部方法!
public class Student extends Person{
}
Application.java
package com.oop;
import com.oop.demo05.Student;
// 一个项目应该只在一个main方法
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
System.out.println();
}
}
1. super
super注意点:
1. super 调用父类的构造方法,必须在构造方法的第一个
2. super 必须只能出现在子类的方法或者构造方法中!
3. super 和 this 不能同时调用构造方法!
Vs this:
代表的对象不同
this:本身调用者这个对象
super:代表父类对象的应用
前提
this:没哟继承也可以使用
super:只能在继承条件才可以使用
构造方法
this();本类的构造
super():父类的构造
代码 :
父类
package com.oop.demo05;
// 在Java 中,所有的类,都默认直接或者间接继承 Object
// Person 人 : 父类
public class Person {
//public
// protected
//default
//private
public Person(){
System.out.println("Person无参执行了");
}
protected String name = "sk";
//私有的东西无法被继承!
public void print(){
System.out.println("Person");
}
}
子类:
package com.oop.demo05;
// 继承
// 学生 is 人 : 派生类,子类
// 子类继了父类,就会加有父类的全部方法!
public class Student extends Person{
public Student(){
//隐藏代码;调用了父类的无参构造
super();//调用父类的构造器,必须要在子类构造器的第一行
System.out.println("Student无参执行了");
}
private String name = "blue";
public void print(){
System.out.println("Student");
}
public void text1(){
print();//Student
this.print();//Student
super.print();//Peron
}
public void text(String name){
System.out.println(name);//可
System.out.println(this.name);//blue
System.out.println(super.name);//sk
}
}
应用:
package com.oop;
import com.oop.demo05.Student;
// 一个项目应该只在一个main方法
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.text("可");
student.text1();
}
}
结果
Person无参执行了
Student无参执行了
可
blue
sk
Student
Student
Person
Process finished with exit code 0
2. 方法重写(重点)
重写: 需要有继承关系,子类重写父类的方法!
1.方法名必须相同
2.参数列表列表必须相同
3.修饰符:范围可以扩大但不能缩小: public> Protected> Default> private
4.抛出的异常:范围,可以被缩小,但不能扩大; classNotException–> Exception(大)
重写: 子类的方法和父类必要一致;方法体不同
为什么需要重写:
1.父类的功能,子类不一定需要,或者不一定满足!
alt + insert : override;
_________________________重写前————————————————————————
=============父类
package com.oop.demo05;
// 重写都是方法的重写,和国性无关
public class B {
public static void test(){
System.out.println("B=>text()");
}
}
===========子类
package com.oop.demo05;
// 继承
public class A extends B{
public static void test(){
System.out.println("A=>text()");
}
}
package com.oop;
import com.oop.demo05.A;
import com.oop.demo05.B;
import com.oop.demo05.Student;
// 一个项目应该只在一个main方法
public class Application {
public static void main(String[] args) {
// 方法的调用只和左边, 定义的数据类型有关
A a = new A();
a.test(); //A
// 父类的引用指向了子类
B b = new A();
b.test();//B
}
}
结果:
A=>text()
B=>text()
_________________________重写后————————————————————————
=============父类==================
package com.oop.demo05;
// 重写都是方法的重写,和国性无关
public class B {
public void test(){
System.out.println("B=>text()");
}
}
=============子类==============================
package com.oop.demo05;
// 继承
public class A extends B{
// Override重写
@Override //注解:有功能的注释!
public void test() {
System.out.println("A=>text()");
}
}
=============应用==============================
package com.oop;
import com.oop.demo05.A;
import com.oop.demo05.B;
import com.oop.demo05.Student;
// 一个项目应该只在一个main方法
public class Application {
// 静态的方法和非静态的方法区别很大!
public static void main(String[] args) {
// 方法的调用只和左边, 定义的数据类型有关
A a = new A();
a.test(); //A
// 父类的引用指向了子类
B b = new A(); ///子类重写父类的方法
b.test();//B
}
}
输出:
A=>text()
A=>text()
Process finished with exit code 0
三、多态
多态注意事项
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常! CLasscastException!
3.一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多(父类,有关系的类)
4.存在条件:
- 继承关系,
- 方法需要重写,
- 父类引用指向子类对象! Father f1= new Son()
不能重写
1. static方法, 属于类, 它不属实例
2. final常量
3. private方法;
5. 类型转换
- instanceof 判断一个对象是什么类型
============Person================================
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("ppp");
}
}
============Student===============================
package com.oop.demo06;
public class Student extends Person{
public void run(){
System.out.println("run");
}
public void eat(){
System.out.println("eat");
}
}
============Application================================
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
// 一个项目应该只在一个main方法
public class Application {
public static void main(String[] args) {
// 一个对象的实际类型是确定的
// new Student();
// new Person();
// 可以指向的引用类型就不确定了 父类的引用指向子类
// Student能调用的方法都是自己的或者继承父类的!
Student s1 = new Student();
// Person父类,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
// 对象能执行哪些方法,主要看对象左边的类型,和右边的关系不大
s2.run(); //子类重写了父类的方法,执行子类的方法
s1.run();
// s2.eat(); // 强制转换
}
}
instanceof
- 父类引用指向子类的对象
- 把子类转换为父类,向上转型
- 把父类转换为子类,向下转型
- 方便方法的调用,减少重复的代码! 简洁
============Person================================
package com.oop.demo06;
public class Person {
}
============Student================================
public class Student extends Person{
public void run(){
System.out.println("run");
}
public void eat(){
System.out.println("eat");
}
public void go(){
System.out.println("go");
}
}
============Teacher================================
package com.oop.demo06;
public class Teacher extends Person{
}
============Application 1================================
public class Application {
public static void main(String[] args) {
// Object > String
// Object > Person > Student
// Object > Person > Teacher
Object object = new Student();
// System.out.println(X instanceof Y); // 能不能编译通过!
System.out.println(object instanceof Student); //true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//Fslse
System.out.println(object instanceof String);//Fslse
System.out.println("===========================================");
Person person = new Student();
System.out.println(person instanceof Student); //true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//Fslse
// System.out.println(person instanceof String); // 编译报错
System.out.println("=============================");
Student student = new Student();
System.out.println(student instanceof Student); //true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
// System.out.println(student instanceof Teacher); // 编译报错
// System.out.println(student instanceof String); // 编译报错
System.out.println("===========================================");
}
}
============Application 2================================
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
// 一个项目应该只在一个main方法
public class Application {
public static void main(String[] args) {
//类型之间的转化: 基本类型转换 高低 64 32 16
// student将这个对象转换为 Student类型,我们就可以使用 Student类坐的方法了!
//类型之间的转化:父---子
// Person obj = new Student();
// ((Student) obj).go();
// 子类转换为父类,可能丢失自己的本来的一些方法!
Student student = new Student();
student.go();
Person person = student;
}
}
static方法
============Student================================
package com.oop.demo07;
public class Student {
private static int age; //静态的变量 多线程
private double score; // 非静态的变量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
go();
// Student.go();
// new Student().run();
// Student s1 = new Student();
// System.out.println(Student.age);
// System.out.println(s1.age);
// System.out.println(s1.score);
}
}
============Person================================
package com.oop.demo07;
public class Person {
{
//代码块(匿名代码块)
System.out.println("匿名代码块");
}
//1 : 只执行一次~
static {
// 静态代码块
System.out.println("静态代码块");
}
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person1 = new Person();
/* 输出
静态代码块
匿名代码块
构造方法
*/
System.out.println("==========================");
Person person2 = new Person();
/* 输出
匿名代码块
构造方法
*/
}
}
=================Test===============================
package com.oop.demo07;
// 静态导入包~
// final 后 断子绝孙
import static java.lang.Math.PI;
import static java.lang.Math.random;
public class Test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}