封装
package com.oop.demo04;
//封装类
//类 private: 私有
public class Student {
//属性私有
private String name; //名字
private int id; //学号
private char sex; //性别
private int age; //年龄
//提供一些可以操作这个属性的方法!
//提供一些public的get、set方法
//get 获得这个数据
public String getName() {
return name;
}
//set给这个数据设置值
public void setName(String name) {
this.name = name;
}
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<0 || age>120){//不合法
this.age = 3;
}else{
this.age = age;
}
}
}
/*
封装:
1。提高程序的安全性,保护数据
2。隐藏代码的实现细节
3。统一接口
4。系统可维护增加了
//执行方法
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("马克吐痰");
System.out.println(s1.getName());
s1.setAge(200);
System.out.println(s1.getAge());
}
*/
继承
继承的本质是对某一批类的抽象,从而实现对现实世界更好的建
extands的意思是“扩展”。子类是父类的扩展。
JAVA中类只有单继承,没有多继承!
继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。
继承关系的俩个类,一个为子类(派生类).一个为父类(基类)。
子类继承父类,使用关键字extends来表示。子类和父类之间,从意义上讲应该具有"is a"的关系.
object类
super
super注意点:
1. super调用父类的构造方法,必须在构造方法的第一个
2. super必须只能出现在子类的方法或者构造方法中!
3. super和 this不能同时调用构造方法!
vs this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的引用
前提
this:没有继承也可以使用
super:只能在继承条件下使用
构造方法的区别:
this();本类的构造
super();父类的构造!
Person();
package com.oop.demo05;
public class Person {
public Person() {
System.out.println("Person的无参构造");
}
public Person(String name) {
this.name = name;
}
protected String name = "chenyan";
public void print(){
System.out.println("Person");
}
}
Student();
package com.oop.demo05;
public class Student extends Person{
public Student() {
super("name");
System.out.println("Student的无参构造");
}
private String name = "aiyinsidan";
public void print(){
System.out.println("Student");
}
public void test1(){
print();//Student
this.print();//Student
super.print();//Person
}
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
}
Application();
package com.oop;
import com.oop.demo05.Person;
import com.oop.demo05.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
// student.test("陈琰");
// student.test1();
}
}
方法重写 重点—>多态
重写:需要有继承关系,子类重写父类的方法!
1.方法名必须相同
2. 参数列表列表必须相同
3.修饰符:范围可以扩为: public>Protected>Default>private
-
抛出的异常:范围,可以被缩小,但不能扩大;
ClassNotFoundException–>Exception(大)
重写,子类的方法和父类必要一致,方法体不同!
为什么需要重写:
-
父类的功能,子类不一定需要,或者不一定满足!
Alt+Insert ; override;
多态
-
即同一方法可以根据发送对象的不同而采用多种不同的行为方式。
-
一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
-
多态存在的条件
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
-
注意:多态是方法的多态,属性没有多态性。
Person()
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
/*
多态注意事项:
1. 多态是方法的多态,实行没有多态
2. 父类盒子类,有联系 类型转换异常! ClassCastException!
3. 存在条件: 继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();
父类不可以引用
1.static 方法,属于类,它不属于实例
2.final 常量;
3.private 方法;
; */
Student()
package com.oop.demo06;
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
- instanceof (类型转换) 引用类型
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
public static void main(String[] args) {
//Object > String
//Object > Person > Teacher
//Object > Person > Student
// System.out.println(X instanceof Y);//能不能编译通过! 接口
Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//ture
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);//ture
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); //ture
System.out.println(student instanceof Object); //true
// System.out.println(student instanceof Teacher);//编译报错!
// System.out.println(student instanceof String); //编译报错!
}
}
true
true
true
false
false
true
true
true
false
true
true
true