JavaSE Day08
1. 封装
属性私有!! private 关键字 get/set方法(alt+insert 自动生成)
1 package com.oop.demo03; 2 3 //类 private:私有 4 public class Student { 5 6 //属性私有 7 private String name;//名字 8 private int id;//学号 9 private char sex;//性别 10 private int age; 11 12 public String getName() { 13 return name; 14 } 15 16 public void setName(String name) { 17 this.name = name; 18 } 19 20 public int getId() { 21 return id; 22 } 23 24 public void setId(int id) { 25 this.id = id; 26 } 27 28 public char getSex() { 29 return sex; 30 } 31 32 public void setSex(char sex) { 33 this.sex = sex; 34 } 35 36 public int getAge() { 37 return age; 38 } 39 40 public void setAge(int age) { 41 if (age > 120 || age < -1) 42 age = 3; 43 else 44 this.age = age; 45 } 46 }
1 package com.oop; 2 3 import com.oop.demo03.Student; 4 5 public class Application { 6 public static void main(String[] args) { 7 Student student1 = new Student(); 8 student1.getName(); 9 student1.setName("azy"); 10 11 student1.setAge(999);//不合法 12 System.out.println(student1.getAge()); 13 } 14 }
封装的好处:
-
提高程序的安全性,保护数据
-
隐藏代码的实现细节
-
统一接口,全是get、set
-
系统可维护性增加了
2. 继承
继承的本质是对某一类的抽象,从而实现对现实世界更好的建模。
继承是类和类之间的关系,子类继承父类,用关键字extends
Java中只有单继承,一个儿子只有一个爸爸,一个爸爸有多个儿子
1 package com.oop.demo04; 2 3 //Person 人 :父类 4 public class Person { 5 public int money = 10_0000_0000; 6 private int money1 = 10000; 7 int money2 = 100;//default 8 protected int money3 = 100000;//保护级最高 9 10 public void say() { 11 System.out.println("aaaa"); 12 } 13 14 public int getMoney1() { 15 return money1; 16 } 17 18 public void setMoney1(int money1) { 19 this.money1 = money1; 20 } 21 }
1 package com.oop.demo04; 2 3 //学生 is 人 :派生类,子类 4 public class Student extends Person { 5 } 6 package com.oop.demo04; 7 8 public class Teacher extends Person { 9 } 10 package com.oop; 11 12 import com.oop.demo04.Student; 13 14 public class Application { 15 public static void main(String[] args) { 16 //Student 继承了Person类! 17 Student s1 = new Student(); 18 s1.say(); 19 System.out.println(s1.money); 20 21 } 22 }
Ctrl+H 打开继承关系
在Java中所有的类,都默认继承object!!
3. private,protect,this,super关键字
super注意点:
-
super调用父类的构造方法,必须在构造器方法的第一个
-
super必须只能出现在子类的方法或者构造方法中!
-
super 和 this 不能同时调用构造方法!
vs this:
-
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
-
前提
this:没有继承也可以使用
super:只能在继承后使用
-
构造方法
this():本类的构造
super():父类的构造
1 package com.oop.demo04; 2 3 //Person 人 :父类 4 public class Person { 5 protected String name = "yc"; 6 7 public void print() { 8 System.out.println("Person"); 9 } 10 }
1 package com.oop.demo04; 2 3 //学生 is 人 :派生类,子类 4 public class Student extends Person { 5 public Student() { 6 //隐藏代码,调用了父类的无参构造 7 super();//必须放在子类构造器的第一行 8 } 9 10 private String name = "azy"; 11 12 public void setName(String name) { 13 System.out.println(name); 14 System.out.println(this.name); 15 System.out.println(super.name); 16 } 17 18 public void print() { 19 System.out.println("Student"); 20 } 21 22 public void test() { 23 print(); 24 this.print(); 25 super.print(); 26 } 27 }
1 package com.oop; 2 3 import com.oop.demo04.Student; 4 5 public class Application { 6 public static void main(String[] args) { 7 8 Student student = new Student(); 9 student.setName("xiaolaji"); 10 11 System.out.println("--------------------"); 12 student.test(); 13 14 } 15 }
输出:
xiaolaji
azy
yc
Student
Student
Person
4. 方法重写
重写都是方法的重写,和属性无关
1 package com.oop.demo04; 2 3 public class B { 4 public static void test(){ 5 System.out.println("B->test"); 6 } 7 }
1 package com.oop.demo04; 2 3 public class A extends B{ 4 public static void test() { 5 System.out.println("A->test"); 6 } 7 }
1 package com.oop; 2 3 import com.oop.demo04.A; 4 5 public class Application { 6 public static void main(String[] args) { 7 8 A a = new A(); 9 a.test(); 10 B b = new A(); 11 a.test(); 12 13 } 14 }
A->test
B->test
1 package com.oop.demo04; 2 3 public class B { 4 public void test(){ 5 System.out.println("B->test"); 6 } 7 }
1 package com.oop.demo04; 2 3 public class A extends B{ 4 @Override //重写 5 public void test() { 6 System.out.println("A->test"); 7 } 8 }
去了static
A->test
A->test
( 因为静态方法是类的方法,而非静态是对象的方法;有static时,b调用了B类的方法,因为b是用b类定义的;没有static时,b调用的是对象的方法,而b是用A类new的;即b是A new出来的对象,因此调用了A的方法 )
得出结论,静态方法和非静态方法区别很大!
静态方法:方法的调用只和左边,定义的数据类型有关
非静态:重写
重写:需要有继承关系,子类重写父类的方法!
-
方法名必须相同
-
参数列表必须相同
-
修饰符:范围可以扩大 public> protected> default> private
-
抛出的异常:范围可以被缩小,但不能扩大 ClassNotFoundException-->Exception(大)
重写,子类的方法和父类必须要一致;方法体不同!
为什么需要重写? 父类的功能子类不一定需要,或者不一定满足 alt+insert ---> override
5. 多态
动态编译:类型(可扩展性)
1 package com.oop.demo05; 2 3 public class Person { 4 public void run(){ 5 System.out.println("run"); 6 } 7 8 }
1 package com.oop.demo05; 2 3 public class Student extends Person{ 4 @Override 5 public void run() { 6 System.out.println("son"); 7 } 8 public void eat(){ 9 System.out.println("eat"); 10 } 11 }
1 package com.oop; 2 3 import com.oop.demo05.Person; 4 import com.oop.demo05.Student; 5 6 public class Application { 7 public static void main(String[] args) { 8 9 //一个对象的实际类型是确定的 10 //可以只想的引用类型就不确定了 11 12 //Student 能调用的方法都是自己的或者是继承父类的 13 Student s1 = new Student(); 14 //Person 父类型,可以执行子类,但是不能调用子类独有的 15 Person s2 = new Student(); 16 Object s3 = new Student(); 17 18 //对象能执行哪些方法,主要看对象左边的类型(Student、Person、Object),和右边关系不大! 19 20 s2.run();//son,子类重写了父亲的方法 21 s1.run();//son 22 s1.eat(); 23 //s2.eat();不可调用 24 ((Student) s2).eat(); 25 } 26 }
多态注意事项:
-
多态是方法的多态,属性没有多态
-
父类和子类,有联系 类型转换异常 ClassCastException
-
多态存在条件:继承关系,方法需要重写,父类引用指向子类对象 Father f1 = new Son();
不能被重写:
-
static 方法不能重写,属于类,它不属于实例
-
final 常量
-
private 方法
6. instanceof 关键字
System.out.println(X instanceof Y); 能不能编译通过,主要是看X和Y 有没有父子关系
1 package com.oop; 2 3 import com.oop.demo05.Person; 4 import com.oop.demo05.Student; 5 import com.oop.demo05.Teacher; 6 7 public class Application { 8 public static void main(String[] args) { 9 10 //Object > String 11 //Object > Person > Student 12 //Object > Person > Teacher 13 Object object = new Student(); 14 15 System.out.println(object instanceof Student);//ture 16 System.out.println(object instanceof Person);//ture 17 System.out.println(object instanceof Object);//ture 18 System.out.println(object instanceof Teacher);//false 19 System.out.println(object instanceof String);//false 20 21 System.out.println("---------------"); 22 Person person = new Student(); 23 System.out.println(person instanceof Student);//ture 24 System.out.println(person instanceof Person);//ture 25 System.out.println(person instanceof Object);//ture 26 System.out.println(person instanceof Teacher);//false 27 // System.out.println(person instanceof String);//编译报错! 28 29 System.out.println("---------------"); 30 Student student = new Student(); 31 System.out.println(student instanceof Student);//ture 32 System.out.println(student instanceof Person);//ture 33 System.out.println(student instanceof Object);//ture 34 // System.out.println(student instanceof Teacher);//编译报错! 35 } 36 }
7. 类型转换
低(子)--->高(父) 向上转型。可以直接转,丢失子类中原本可以直接调用的特有方法
高(父)--->低(子) 向下转型。需要强转,丢失父类被子类所重写的方法
1 package com.oop; 2 3 import com.oop.demo05.Person; 4 import com.oop.demo05.Student; 5 import com.oop.demo05.Teacher; 6 7 public class Application { 8 public static void main(String[] args) { 9 //类型之间的转换: 父 子 10 11 12 Person student = new Student(); 13 //低(子)--->高(父) 向上转型。可以直接转,丢失子类中原本可以直接调用的特有方法 14 //高(父)--->低(子) 向下转型。需要强转,丢失父类被子类所重写的方法 15 Person person = student; 16 17 //student.go();//不能通过 18 //将student这个对象转化为Student类型,我i们就可以使用Student类型的方法了! 19 20 ((Student) student).go(); 21 } 22 }

浙公网安备 33010602011771号