面向对象08:封装详解
package com.oop.demo04;
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 > 120 || age < 0){
this.age = 3;
}else {
this.age = age;
}
}
}
//测试
package com.oop;
import com.oop.demo04.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.setName("feifei");
student.setId(15216062);
student.setSex('男');
student.setAge(150);
System.out.println(student.getName());
System.out.println(student.getId());
System.out.println(student.getSex());
System.out.println(student.getAge());
}
}
封装的作用:
- 提高程序的安全性,保护数据
- 隐藏代码的实现细节
- 统一接口
- 系统的可维护性增加了
面向对象09:什么是继承
回顾:在Java中判断一个类中的两个方法是否相同,主要参考两个东西:1、方法名;2、参数列表
package com.oop.demo05;
//Person 人:父类
//在Java中,所有类,都默认的直接或间接的继承Object类
public class Person {
//优先级从高到低
//public
//protected
//default
//private
private int money = 10_0000_0000;
public void say(){
System.out.println("说了一句话!");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
package com.oop.demo05;
//老师 is 人:子类、派生类
//子类继承父类,就会拥有父类全部的方法
public class Teacher extends Person {
}
//测试
package com.oop;
import com.oop.demo05.Teacher;
public class Application {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.say();
}
}
面向对象10:Super()详解
调用属性
package com.oop.demo06;
public class Person {
protected String name = "feifei";
}
//子类
package com.oop.demo06;
public class Student extends Person {
private String name = "xiaofei";
public void test(String name){
System.out.println(name); //飞飞
System.out.println(this.name); //xiaofei
System.out.println(super.name); //feifei
}
}
//测试
package com.oop;
import com.oop.demo05.Teacher;
import com.oop.demo06.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("飞飞");
}
}
调用方法
package com.oop.demo06;
public class Person {
protected String name = "feifei";
//私有的东西无法被继承
public void say(){
System.out.println("Person类");
}
}
//子类
package com.oop.demo06;
public class Student extends Person {
private String name = "xiaofei";
public void say(){
System.out.println("Student类");
}
public void test(String name){
System.out.println(name); //飞飞
System.out.println(this.name); //xiaofei
System.out.println(super.name); //feifei
}
public void test1(){
say(); //Student类
this.say(); //Student类
super.say(); //Person类
}
}
//测试
package com.oop;
import com.oop.demo05.Teacher;
import com.oop.demo06.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("飞飞");
student.test1();
}
}
无参构造和有参构造
package com.oop.demo06;
public class Person {
public Person() {
System.out.println("父类的无参构造器");
}
}
//子类
package com.oop.demo06;
public class Student extends Person {
private String name;
public Student(){
//隐藏代码:默认调用了父类的无参构造
//即下行这句代码
super();//调用父类构造器,必须要在子类构造器的第一行,调用this构造器也是同理
//且this();和super();只能调用一个
System.out.println("子类的无参构造器");
}
public Student(String name) {
this.name = name;
}
}
//测试
package com.oop;
import com.oop.demo05.Teacher;
import com.oop.demo06.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
}
}
输出结果为:
父类的无参构造器
子类的无参构造器
显式的调用父类有参
package com.oop.demo06;
public class Person {
public Person(String name) {
System.out.println("父类的无参构造器");
}
}
//子类
package com.oop.demo06;
public class Student extends Person {
private String name;
public Student(){
//隐藏代码:默认调用了父类的无参构造
//即下行这句代码
super("name");//调用父类构造器,必须要在子类构造器的第一行,调用this构造器也是同理
//且this();和super();只能调用一个
System.out.println("子类的无参构造器");
}
}
//测试
package com.oop;
import com.oop.demo05.Teacher;
import com.oop.demo06.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
}
}
总结
super注意点:
- 调用父类的构造方法,且必须在构造方法的第一个
- super必须智能出现在子类的方法或者构造方法中
- super和this不能同时调用构造方法
super vs this
-
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的引用
-
前提:
this:没有继承也可以使用
super:只能在继承条件下才可以使用
-
构造方法:
this(); 本类的构造
super();父类的构造
面向对象11:方法重写
静态方法
package com.oop.demo07;
//重写都是方法的重写,和属性无关
public class A {
public static void test(){
System.out.println("A --> test()");
}
}
//子类
package com.oop.demo07;
public class B extends A{
public static void test(){
System.out.println("B --> test()");
}
}
//测试
package com.oop;
import com.oop.demo07.A;
import com.oop.demo07.B;
public class Application {
//静态方法和非静态方法区别很大
//静态方法:方法的调用只和左边定义的数据类型有关,A调用的A,B调用的B
public static void main(String[] args) {
//方法的调用只和左边定义的数据类型有关,A调用的A,B调用的B
B b = new B();
b.test(); //B
//这叫:父类的引用指向了子类
A a = new B();
a.test(); //A
//B类继承了A类
}
}
非静态方法
package com.oop.demo07;
//重写都是方法的重写,和属性无关
public class A {
public void test(){
System.out.println("A --> test()");
}
}
//子类
package com.oop.demo07;
public class B extends A{
@Override //注解,有功能的注释
public void test() {
System.out.println("B --> test()");
}
}
//测试
package com.oop;
import com.oop.demo07.A;
import com.oop.demo07.B;
public class Application {
//非静态方法才可以重写,且必须为public
public static void main(String[] args) {
B b = new B();
b.test(); //B
A a = new B(); //子类重写父类方法
a.test(); //B
//B类继承了A类
}
}
两个程序的区别:因为静态方法是类的方法,而非静态方法是对象的方法。所以在有static时,a调用了A类的方法,因为a是用A类定义的;没有static时,a调用的是对象的方法,而a是用B类new出来的,即a是B new出来的对象,因此调用了B方法。
总结
重写:需要有继承关系,且子类重写父类的方法!
- 方法名必须相同
- 参数列表必须相同
- 修饰符:范围可以扩大,但不能缩小 public > protected > default > private
- 抛出的异常:范围可以被缩小,但不能扩大
重写:子类的方法名必须要和父类一致,方法体不同
为什么需要重写?
- 父类的功能子类不一定需要,或者不一定满足
- Alt + Insert: override