面向对象02
封装
-
我们程序设计要求高内聚,低耦合,高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。
属性私有get/set
//类 private:私有
public class Student {
//属性私有
private String name;//名字
private int id;//学号
private char sex;//性别
//提供一些可以操作这个属性的方法
//提供一些public的get、set的方法
//get获得这个数据
public String getName(){
return this.name;
}
//set给这个数据设置值
public void setName(String name){
this.name=name;
}
//alt+insert自动生成get、set方法,对属性
}
/*
封装的意义:
1.提高程序的安全性,保护数据
2.可以隐藏代码的实现细节
3.统一接口
4.系统的可维护性增加
*/
继承
-
继承的本质是对某一批类的抽象
extends的意思是“扩展”,子类是父类的扩展
- 继承是类和类之间的一种关系,除此之外,类和类之间的关系还有依赖、组合、聚合等
- 继承关系的两大类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示
- 子类和父类之间可用“is a”表示
注意
四个修饰符
public protected default private
Java中只有单继承,没有多继承
-
子类继承了父类,就会拥有父类的全部方法
-
在java中所有类都直接或者间接继承object类
-
私有的东西无法被继承
super
-
调用父类的构造器,必须要在子类的构造器第一行,super()
-
注意点:
-
super调用父类的构造方法,必须在构造方法的第一个
-
super必须只能出现在子类的方法或者构造方法中!
-
super和this 不能同时调用构造方法
-
Vs this
-
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提:
this:没有继承也可以使用
super:只能在继承条件才可以使用
构造方法
this():本类的构造
super():父类的构造
方法重写
-
重写都是方法的重写,和属性无关
public class Application {
public static void main(String[] args) {
//静态的方法和非静态的方法区别很大
//静态方法//方法的调用只和左边定义的数据类型有关
//非静态方法:重写
A a = new A();
a.test();//A类的方法
//父类的引用指向子类
B b= new A();//子类重写了父类的方法
b.test();//B类的方法
}
}
/*
重写:需要有继承关系,子类重写父类的方法!
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大,但不能缩小。public>protected>default>private
4.抛出的异常:范围可以被缩小,但不能扩大 ClassNotFoundException--》Exception(大)
重写,子类的方法和父类必须一致;方法体不同
为什么需要重写:
1.父类的功能子类不一定需要,或者不一定满足
2.alt+insert:override;
*/
public class A extends B{
public void test(){
System.out.println("A=>test()");
}
} -
-
public class B {
public void test(){
System.out.println("B=>test()");
}
}
多态
-
即同一个方法可以根据发送对象的不同而采取多种不同的行为方式。
-
一个对象的实际类型是确定的,但可以指向对象的引用类型有很多(父类,有关系的类)
-
多态存在条件
-
有继承关系
-
子类重写父类方法
-
父类引用指向子类对象
-
注意:多态是方法的多态,属性没有多态性
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();//
//对象能执行哪些方法,主要看对象左边的类型,和右边的关系不大
s1.eat();
((Student)s2).eat();//强转换(高转低)
}
}
public class Person {
public void run(){
System.out.println("run");
}
}
public class Student extends Person{
instanceof 和类型转换
-
instanceof(类型转换)引用类型,判断一个对象是什么类型·
-
instanceof
import com.oop.day6.Person;
import com.oop.day6.Student;
import com.oop.day6.Teacher;
public class Application {
public static void main(String[] args) {
//object>string
//object>person>teacher
//object>person>student
//System.out.println(x instanceof y);//能不能编译通过取决于x于y是否存在父子关系
Object object = new Student();
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);//false
System.out.println(object instanceof String);//false
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);//false
//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);//编译曝错
}
} -
类型转换
mport com.oop.day6.Person;
import com.oop.day6.Student;
public class Application {
public static void main(String[] args) {
//类型之间的转换:父子
//高 低
Person obj = new Student();
//student将这个对象转换为student类,我们就可以使用student类的方法
Student student = (Student)obj;
student.go();
((Student)obj).go;
//子类转换为父类,可能丢失自己本来的一些方法!
}
}
、
/*
1。父类的引用指向子类的对象
2.把子类转换为父类,向上转型
3.把父类转换为子类,向下转型;强制转换
4,方便方法的调用。减少重复的代码!简介
抽象:封装,继承,多态
*/

浙公网安备 33010602011771号