Java学习中关于面向对象非常重要的三个知识
1.封装 2.继承 3.多态
封装
封装:
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统可维护增加
- 代码
头部
public class opp2 {
public static void main(String[] args) {
People student = new People();
student.setAge(300);
System.out.println(student.getAge());
student.setName("jek");
System.out.println(student.getName());
student.setWeight(120.5);
System.out.println(student.getWeight());
}
}
对象
public class People {
//private保证了数据的安全性
private int age;
private String name;
private double weight;
//get与set用于student间接访问私有
public int getAge() {
return age;
}
public void setAge(int age) {
//实现内部对数据检测是否非法长度
if (age < 120 && age > 0 ){
this.age = age;
}else {
this.age = 1;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
继承
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现在子类的方法或者构造方法中!
3.super和this不能同时调用构造方法
Vs this
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提
this:没有继承也可以用
super:只能在继承条件子下才可以使用
构造方法
this();本类的构造
super();父类的构造
- 代码
头部
public class opp03 {
public static void main(String[] args) {
System.out.println("子类可以直接访问父类的方法,但是不能直接访问私有成员,需要间接访问:"+"\n");
Son people = new Son("吃饭");
people.setMoney(1000);
System.out.println(people.hobby);
System.out.println("======================");
System.out.println("当构造无参时,先调用父类构造的然后在调用子类的构造:"+"\n");
Son people1 = new Son();
System.out.println("=======================");
System.out.println(people.getMoney());
System.out.println("======================");
System.out.println("在子类直接调用父类的方法,使用super访问:"+"\n");
people.test2();
}
}
父类
public class Father {
private int money;
private String life;
public String daily;
public Father() {
System.out.println("父亲");
}
public Father(String daily) {
this.daily = daily;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
if (money > 0){
this.money = money;
}else {
this.money = 0;
}
}
public String getLife() {
return life;
}
public void setLife(String life) {
this.life = life;
}
public void test1(){
System.out.println("我是你爸爸");
}
}
子类
public class Son extends Father {
//子类中父类的私有,说明子类能够继承父类的私有属性,只是因为权限问题无法访问
//公有的父类可以调用
private String study;
public String hobby;
public Son() {
//此地方有一个隐藏的调用super(),自动调用父类的构造
System.out.println("儿子");
}
public Son(String hobby) {
this.hobby = hobby;
}
public String getStudy() {
return study;
}
public void setStudy(String study) {
this.study = study;
}
public void desk(){
System.out.println("我是儿子的坐姿");
}
public void test2(){
this.desk(); //自己的方法调用
super.test1(); //调用父类的方法
}
}
多态
1.多态是方法的多态,属性没有多态
2.父类和子类有联系 类型转换异常classCastException!
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象!
4.static方法:不属于实例,属于类
- 代码
父类
public class People {
public void foot(){
System.out.println("两只脚");
}
}
子类
public class Student extends People {
public void foot(){
System.out.println("八只脚");
}
public void eat(){
System.out.println("淦饭");
}
}
主头
public class oop05 {
public static void main(String[] args) {
//能调用的都是自己的或者继承父类的!
Student student1 = new Student();
//父类可以指向子类,但是不能调用子类特有的方法!
People student2 = new Student();
Object student3 = new Student();
//对象能执行哪些方法,主要看左边的类型,和右边关系不大!
student1.foot();
student1.eat();
System.out.println("==============");
student2.foot();
//student2.eat()不能重写父类,原因是在父类中并没有eat()这个方法
((Student)student2).eat(); //想要调用得使用强制转换
}
}
重写:需要有继承关系,子类重写父类的方法
1.方法名必须相同
2.参数的列表必须相同
3.修饰符:范围可以扩大但不能缩小:public>protect>default>private
4.抛出异常:范围,可以被缩小,但是不能扩大: classNotFoundException --> Exception(大)
重写,子类的方法和父类必须要一致:方法体不同
为什么需要重写:
1.父类的功能,子类不一定需要,或者不一定满足
final:
1.final修饰的类:不能被继承
2.final修饰的方法:不能被重写
3.final修饰的变量、属性:不能被修改
评论区