面向对象编程03:封装、继承、多态
文章目录
封装
- 该露的露、该藏的藏
- 设计程序要追求“高内聚、低耦合”。
- 高内聚就是类的内部数据操作细节由自己完成,不允许外部干涉;
- 低耦合:仅仅暴露少量的方法给外部使用。
- 封装(数据的隐藏)
- 通常我们应该禁止直接访问一个对象中数据的实际表示,而应该通过操作接口来访问,这被称为“信息隐藏”。
- 理解封装,记住这句话就够了:属性私有,get/set
下面我们来看看封装的代码实例:
public class Student {
//属性私有:private
private String name;//名字
private int id;//学号
private char sex;//性别
private int age;//年龄
//提供一些可以操作这个属性的方法:public的get、set方法
//Alt+Enter快捷创建get和set方法
//get 获得这个数据
//set 设置值
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age<0||age>120){
System.out.println("invalid number");
}else{
this.age= age;
}
}
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.setName("David");//将David通过setName方法赋值给name.
System.out.println(student.getName());
student.setAge(20);
System.out.println(student.getAge());
}
}
输出结果:
David
20
封装的好处:
- 提高程序的安全性。保护数据
- 隐藏代码的实现细节
- 统一接口
- 增加了系统的可维护性
继承
- 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模。
- extends的意思是“扩展”。子类是父类的扩展。
- JAVA中类只有单继承,没有多继承!
- 继承是类与类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。
- 继承关系的两个类:一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends表示。
- 子类和父类之间从意义上讲应该具有“is a”的关系。
下面我们来看一个简单的继承的例子:
//Java中所有的类都默认直接或间接继承object类
//父类: Person
class Person /*extends object*/ {
int money = 10_000_000;
public void say(){
System.out.println("说了一句话");
}
}
//子类:Student
//继承了父类的所有方法
class Student extends Person {
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();//say是父类Person的方法
System.out.println(student.money);//money是父类Person中的变量
}
}
object类
在Java中所有的类都默认直接或间接继承object类。
class Person /*extends object*/ {
}
java的Object类方法如下:
getClass
方法
获取运行时类型,返回值为Class对象
hashCode
方法
返回该对象的哈希码值,是为了提高哈希表的性能(HashTable)
equals
方法
判断两个对象是否相等,在Object源码中equals就是使用去判断,所以在Object中equals是等价于的,但是在String及某些类对equals进行了重写,实现不同的比较。
clone
方法
主要是JAVA里除了8种基本类型传参数是值传递,其他的类对象传参数都是引用传递,我们有时候不希望在方法里将参数改变,这时就需要在类中复写clone方法。
如果在clone方法中调用super.clone()方法需要实现Cloneable接口,否则会抛出CloneNotSupportedException。
此方法只实现了一个浅层拷贝,对于基本类型字段成功拷贝,但是如果是嵌套对象,只做了赋值,也就是只把地址拷贝了,所以没有成功拷贝,需要自己重写clone方法进行深度拷贝。
5.toString
方法
返回一个String字符串,用于描述当前对象的信息,可以重写返回对自己有用的信息,默认返回的是当前对象的类名+hashCode的16进制数字。
wait
方法
多线程时用到的方法,作用是让当前线程进入等待状态,同时也会让当前线程释放它所持有的锁。直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,当前线程被唤醒
notify
方法
多线程时用到的方法,唤醒该对象等待的某个线程
notifyAll
方法
多线程时用到的方法,唤醒该对象等待的所有线程
9.finalize
对象在被GC释放之前一定会调用finalize方法,对象被释放前最后的挣扎,因为无法确定该方法什么时候被调用,很少使用。
super
- super关键字指向父类:
//父类: Person
class Person {
int age = 60;
public void say(){
System.out.println("Person说了一句话");
}
}
//子类:Student
//继承了父类的所有方法
class Student extends Person{
public void print() {
System.out.println("Peosin is"+ super.age);
System.out.println("Student");
super.say();//
}
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.print();
}
}
输出结果:
Person is 60
Student
Person说了一句话
- 调用子类构造器时相当于提前执行了一行隐藏代码:
super();
:
//父类: Person
class Person {
public Person() {
System.out.println("Person无参构造已执行");
}
}
//子类:Student
//继承了父类的所有方法
class Student extends Person{
//super();
public Student() {
System.out.println("Student无参构造已执行");
}
}
/**
* @author 15065170282
*/
public class Application{
public static void main(String[] args) {
Student student = new Student();
}
}
输出结果:
Person无参构造已执行
Student无参构造已执行
super注意点:
super
调用父类的构造方法,必须在构造方法的第一个;super
只能出现在子类的方法或者构造方法中;super
和this
不能同时调用构造方法!
super
VS this
:
-
二者代表对象不同:
this
: 代表调用者本身super
: 代表父类应用 -
使用前提不同:
this
: 不用继承就能直接使用super
: 只能在继承条件下使用 -
构造方法:
this();
代表本类的构造super();
代表父类的构造
方法重写
-
前提:方法重写需要有继承关系,即子类重写了父类的***方法***,方法名、参数都一致但是方法体不同
-
方法重写只针对于实例方法,静态方法不存在重写
-
方法重写只针对于public关键字修饰的方法,private修饰的无法被继承
-
参数列表必须相同(不同就变成重载了)
-
修饰符:范围可以扩大但不能缩小: public>protected>default>private
-
抛出的异常范围可以被缩小但是不能扩大:
-
为什么需要重写?
因为有时父类的功能子类不一定需要或不一定满足;
-
快捷键(IDEA中):
Alt + Insert
class Person{
public void say(){
System.out.println("父亲说了一句话");
}
}
class Student extends Person{
@Override
public void say() {
System.out.println("Student 重写了Person的方法");
}
}
/**
* @author 15065170282
*/
public class OverrideTest {
public static void main(String[] args) {
Person person = new Person();
person.say();
Student student = new Student();
student.say();
}
}
多态
- 同一方法可以根据发送对象的不同采取多种不同的行为方式。
- 一个对象的实际类型是确定的,但可以指向该对象的引用类型有很多
- 多态存在的条件:
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
下面来看一个多态的实例:
package JavaSe.oop.demo07;
class Person{
public void run(){
System.out.println("run");
}
}
class Student extends Person{
@Override
public void run() {
System.out.println("子类重写了父类的run方法");
}
public void eat(){
System.out.println("eat");
}
}
/**
* @ClassName Application
* @author 15065170282
**/
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();
//对象能执行哪些方法取决于引用类的类型,和实现类无关;
s1.eat();
((Student) s2).eat();//类型转换
s1.run();
s2.run();
}
}
输出结果:
eat
eat
子类重写了父类的run方法
子类重写了父类的run方法
多态的注意事项:
-
多态指的是方法的多态,属性没有多态一说。
-
父类和子类因为有联系(继承关系)才能进行转换
类型转换异常:ClassCastException!
-
存在条件: 继承关系,方法需要重写,父类引用指向子类对象: Father F1 = new Son();
无法重写:
static
方法,属于类,不属于实例;final
常量;private
方法;
instanceof关键字
instanceof 关键字用于判断两个类之间是否有父子关系。
public class InstanceOfTest {
public static void main(String[] args) {
//System.out.println(X instanceof Y);//能否编译通过取决于X与Y之间是否有关
//object-->Person-->Student
//object-->Person-->Teacher
Object o = new Object();
System.out.println(o instanceof Student);
System.out.println(o instanceof Person);
System.out.println(o instanceof Object);
System.out.println(o instanceof Teacher);
System.out.println(o instanceof String);
System.out.println("====================================");
Person person = new Person();
System.out.println(person instanceof Student);
System.out.println(person instanceof Person);
System.out.println(person instanceof Object);
System.out.println(person instanceof Teacher);
//编译时就会报错:System.out.println(person instanceof String);
System.out.println("====================================");
Student student = new Student();
System.out.println(student instanceof Student);
System.out.println(student instanceof Person);
System.out.println(student instanceof Object);
//编译时就会报错:System.out.println(student instanceof Teacher);
//编译时就会报错:System.out.println(student instanceof String);
}
}
输出结果:
false
false
true
false
false
====================================
false
true
true
false
====================================
true
true
true
父类与子类之间的类型转换
- 父类引用指向子类的对象: Father f = new Son();
- 子类转换成父类,向上转型;
- 父类转换成子类,向下转型:强制转换
- 作用:方便方法的调用,减少代码的重复
下面来看父子类类型转换的代码实例:
class Test{
}
class TestSon extends Test{
public void go(){
System.out.println("go");
}
}
/**
* @ClassName Application
* @Author ${17368877923}
**/
public class Application {
public static void main(String[] args) {
//类型之间的转化: 父 子
//高------>低需要强转
//低------>高可以直接转
Test test = new TestSon();
new TestSon().go();//对象.方法
//test.go();
TestSon testSon = (TestSon) test;
//可以对比int testSon=3; double test =3.0; int testSen=(int) test;高到低强转
// testSon将test对象转换为TestSon类型,我们就可以使用TestSon类型的方法了
testSon.go();
((TestSon) test).go();
}
}