封装
禁止直接访问一个对象中数据的时间表现 改露的接口露 改藏的数据藏好
追求: 高内聚 低耦合
封装可以防止给的数据不合法进行提前判断
public class Student {
//属性 //priavte 关键字 让属性私有
private String name; //外面的类无法控制或者获得此属性
private int age;
//get set 方法来获得和操作
public String getName() {//获取名字
return name;
}
public void setName(String name) {//设置名字
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) { // 防止年龄不合法
if(age>120||age<-1){ //如果年龄大于120 或者小于-1 自动设置为0
this.age = 0;
}else{
this.age = age;
}
}
}
public static void main(String[] args) {
Studet student = new student();
student.getName(); //来获得名字
student.setName("大漂亮") //设置名字
student.setAge(1000); //因为不合法自动赋值为0
}
继承
关键字 extands
只可以单继承 也就是只有一个父类
子类继承父类 子类会获得父类拥有的方法
在Java中所有的类都继承Object类会自动继承
Teacher, Student 都继承 Person 类
public class Person{
private String name; //子类无法获得父类priavte的属性但是可以用get set来操作
public void speak(){
System.out.println("Chinese");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Student extends Person{
}
public class Teacher extends Student{
}
public static void main(String[] args) {
Student student1 = new Student();
student1.speak(); //student 里没有speak 但是继承了person类就继承了 Person的方法
}
public static void main(String[] args) { //依然可以继承 多继承
Teacher teacher = new Teacher();
teacher.speak();
}
super
public class Person{
protected String name ="person";
}
public class Student extends Person{
private String name ="student";
public void printName(String name){
System.out.println(name); //打印形式参数name给的值
System.out.println(this.name);//打印student
System.out.println(super.name);//打印person
}
}
public static void main(String[] args) {
Student s = new Student();
s.printName("main");
}
如果想用super(); 来调用父类的构造器必须在子类构造器第一行不可以在其他地方 一般自动调用父类无参
注意:
-
super 只能在子类构造方法内
-
super 和 this 不可以同时调用构造方法
-
super 必须在构造器第一行
继承重写
public class Person{
public void printName(){
System.out.println("person");
}
}
public class Student extends Person{
public void printName(){
System.out.println("student");
}
}
public static void main(String[] args) {
Student s = new Student();
s.printName(); //会打印 student
}
继承构造
public class Person{
public void printName(){
System.out.println("person");
}
}
public class Student extends Person{
public void printName(){
System.out.println("student");
}
}
public static void main(String[] args) {
Student s = new Student();
s.printName(); //会打印 student
}
继承中的构造器
//会先运行父类构造器在运行子类
public class Person{
public Person(){
System.out.println("person");
}
}
public class Student extends Person{
public Student(){
System.out.println("student");
}
}
public static void main(String[] args) {
Student s = new Student();
//会打印 person 在打印 student
}
继承关系(重写)
重写:
-
方法名必须相同
-
要有继承关系才可以
-
参数列表必须相同
-
修饰符只能扩大不可以缩小
-
抛出异常 范围只能被缩小不可以扩大 ClassNotFoundException
static
A 继承 B 方法调用之和左边又关 static 静态
public class A extends B{
public void print(){
System.out.println("A")
}
}
public class B{
public void print(){
System.out.println("B")
}
}
A a = new A(); //会带哦用A 类 方法 也就是子类方法
B b = new A(); //父类的引用指向了子类 子类方法向上延申
a.print(); //输出 A
b.print(); //输出 B
非static
方法以右边为准
public class A extends B{
public void print(){
System.out.println("A")
}
}
public class B{
public void print(){
System.out.println("B")
}
}
public static void main(String[] args) {
A a = new A();
B b = new B(); //重写了父类方法
a.print(); //输出 A
b.print(); //输出 A
}
为什么需要重写
父类里的功能子类不一定需要或者不满足子类的要求。
多态
同一个事物不同的状态 其实就是方法的多态 属性无法多态
要有继承关系
对象能执行那些方法要看左边
public class Person{
public void printName(){
System.out.println("person");
}
}
public class Student extends Person{
public void printName(){
System.out.println("student");
}
public void speak(){
System.out.println
}
}
public static void main(String[] args) {
//对象的类型是确定的
new student();
new Person();
//对象的类型不确定 //必须要继承关系 父类 new子类
Student student1 = new Student();//调用子类方法和继承的所有方法
Person stuednt2 = new Student();//父类可以指向子类 但是不可以调用只有在子类里的方法
student2.printName();//输出 student
student2.speak();// 不可以调用
//强制转换来调用
((Student) student2).speak(); 此时可以调用
}