创建对象内存分析
封装
高内聚,低耦合
属性私有。get/set
package com.han.oop.demo04; //类 private:私有 public class Student { //属性 private String name; private int id; private char sex; private int age; public int getAge() { return age; } public void setAge(int age) { if(age<0||age>120){ this.age = 3; }else{ this.age = age; } } public String getName() { return name; } 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; } //用get,set操作属性(get获得数据,sey给数据设置值),不操作的话Application不能调用 }
package com.han.oop.demo04; /* 封装的作用: 1.提高程序的安全性,保护数据 2.隐藏代码的实现细节 3.统一接口 4.系统可维护增加了 */ public class Application { public static void main(String[] args) { Student s1 = new Student(); s1.setName("lxh"); System.out.println(s1.getName()); s1.setAge(999); System.out.println(s1.getAge()); s1.setAge(21); System.out.println(s1.getAge()); } }
继承
extends是“扩展”。子类是父类的拓展。
继承是类与类之间的关系。
Java中类只有单继承,一个儿子只有一个爸爸,一个爸爸可以有多个儿子。
Object类是默认类。
私有的东西无法被继承。
object
package com.han.oop.demo05; //在Java中,所有的类,默认继承Object类 public class Person { public int money = 10_0000_0000; public void say(){ System.out.println("说了一句话"); } }
package com.han.oop.demo05; //子类调用父类,就会调用父类的全部方法 public class Student extends Person{ }
package com.han.oop.demo05; public class Application { public static void main(String[] args) { Student student = new Student(); student.say(); System.out.println(student.money); } }
super
package com.han.oop.demo05; //在Java中,所有的类,默认继承Object类 public class Person { protected String name = "lvxuehan"; public void print(){ System.out.println("Person"); } }
package com.han.oop.demo05; //子类调用父类,就会调用父类的全部方法 public class Student extends Person{ private String name = "lxh"; public void print(){ System.out.println("Student"); } public void test1(){ print(); this.print(); super.print(); } public void test(String name){ System.out.println(name);//吕雪涵 System.out.println(this.name);//lxh System.out.println(super.name);//lvxuehan } }
package com.han.oop.demo05; public class Application { public static void main(String[] args) { Student student = new Student(); student.test("吕雪涵"); student.test1(); } }
package com.han.oop.demo05; //在Java中,所有的类,默认继承Object类 public class Person { public Person() { System.out.println("Person无参构造"); } }
package com.han.oop.demo05; //子类调用父类,就会调用父类的全部方法 public class Student extends Person{ public Student() { //隐藏代码,调用了父类的无参构造 super();//调用父类的构造器,必须要在子类的第一行//可以不写,默认 //this.调用的时候也得在第一行 System.out.println("Student无参构造"); } private String name = "lxh"; public void print(){ System.out.println("Student"); } public void test1(){ print(); this.print(); super.print(); } public void test(String name){ System.out.println(name);//吕雪涵 System.out.println(this.name);//lxh System.out.println(super.name);//lvxuehan } }
package com.han.oop.demo05; public class Application { public static void main(String[] args) { Student student = new Student(); //student.test("吕雪涵"); //student.test1(); } }
注意点:
super调用父类的构造方法,必须在构造方法的第一个;
super必须只能出现在子类的方法或者构造方法中;
super和this不能同时调用构造方法
VS this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提:
this:没有继承也可以使用
super:只能在继承条件才可以使用
构造方法:
this();本类的构造
super();父类的构造
方法的重写
需要有继承关系,子类重写父类的方法
子类父类方法名一样;方法体不同
参数列表必须相同;
修饰符:范围可以扩大不可以缩小:public>protected>Default>private
抛出的异常:范围可以缩小不能扩大。
为什么需要重写:父类的功能子类不一定需要,或者不满足。
Alt+Insert:override
静态:方法的调用只和左边定义的数据类型有关
package com.han.oop.demo05; //继承 public class A extends B{ public static void test(){ System.out.println("A=>test()"); } }
package com.han.oop.demo05; //重写都是方法的重写,和属性无关 public class B { public static void test(){ System.out.println("B=>test()"); } }
package com.han.oop.demo05; public class Application { public static void main(String[] args) { //方法的调用只和左边定义的数据类型有关 A a = new A(); a.test();//A //父类的引用指向了子类 B b = new A(); b.test();//B } }
非静态:子类重写了父类的方法(重写不能用private)
package com.han.oop.demo05; //继承 public class A extends B{ // @Override重写 @Override//注释,有功能的注释 public void test() { System.out.println("A=>test()"); } }
package com.han.oop.demo05; //重写都是方法的重写,和属性无关 public class B { public void test(){ System.out.println("B=>test()"); } }
package com.han.oop.demo05; public class Application { public static void main(String[] args) { //方法的调用只和左边定义的数据类型有关 A a = new A(); a.test();//A //父类的引用指向了子类 B b = new A();//子类重写了父类的方法 b.test();// } }
多态
同一方法可以根据发送对象的不同而采用多种不同的行为方式。
package com.han.oop.demo06; public class Person { public void run(){ System.out.println("run"); } }
package com.han.oop.demo06; public class Student extends Person{ @Override public void run() { System.out.println("son"); } public void eat(){ System.out.println("eat"); } }
package com.han.oop.demo06; public class Application { public static void main(String[] args) { //一个对象的实际类型是确定的 //可以指向的引用类型就不确定了,父类的引用指向子类 Student s1 = new Student();//Student能调用的方法都是自己的或者继承父类的 Person s2 = new Student();//Person是父类。可以指向子类,不能调用子类的方法 Object s3 = new Student(); s2.run();//子类重写了父类,执行子类 s1.run(); s1.eat(); //s2.eat();//能执行那些方法主要看对象左边的类型,和左边关系不大Student s1 = new Student(); // Person s2 = new Student();这两个Student、Person中有的方法 } }
多态是方法的多态,属性没有多态;
父类和子类有联系,类型转换异常ClassCastException;
存在条件:继承关系,方法要重写 ,父类引用指向子类;如下分析为例:
子类父类都存在S1时调用子类的,如子类存在则调用子类的,父类不存在,可强制转换为子类型来调用。
Student s1 = new Student();//Student能调用的方法都是自己的或者继承父类的 Person s2 = new Student();//Person是父类。可以指向子类,不能调用子类的方法