封装
package com.oop.demo04;
//类 private:私有
public class Student {
//属性私有
private String name;//名字
private int id;//学号
private char sex;//性别
private int age;
//提供一些可以操作这个属性的方法
//提供一些public的get、set方法
//get 获得这个数据
public String getName(){
return this.name;
}
//set 给这个数据设置值
public void setName(String name){
this.name = name;
}
//Alt + insert
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;
}
}
}
/*
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("棘布");
System.out.println(s1.getName());
s1.setAge(999);//不合法的
System.out.println(s1.getAge());
}
* */
/*
封装的意义:
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.提高系统可维护性
*/
继承
一个儿子只能有一个爸爸,但是一个爸爸可以有多个儿子
object
package com.oop.demo05;
//在Java中,所有的类,都默认直接或者间接继承object类,
//Person 人 父类
public class Person /*extends Object*/ {
//public 公共的
//protected 受保护的h
//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;
//Student is 人 派生类,子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends Person{
public static void main(String[] args) {
Student student = new Student();
student.say();
}
//快捷键:Ctrl + H
}
package com.oop.demo05;
//Teacher is 人 派生类,子类
public class Teacher extends Person{
public static void main(String[] args) {
}
}
package com.oop;
import com.oop.demo05.Person;
public class Application {
public static void main(String[] args) {
Person person = new Person();
}
}
*/
super:
package com.oop.demo05;
//在Java中,所有的类,都默认直接或者间接继承object类,
//Person 人 父类
public class Person /*extends Object*/ {
public Person() {
System.out.println("Person无参执行了");
}
protected String name = "棘布";
//私有的东西无法被继承
public void print(){
System.out.println("Person");
}
}
/*
package com.oop.demo05;
//Student is 人 派生类,子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends Person{
public Student() {
//隐藏代码:默认调用了父类的无参构造
super();
//调用父类的构造器,必须要在子类构造器的第一行
System.out.println("Student无参执行了");
}
private String name = "李骏";
public void print(){
System.out.println("Student");
}
public void test1(){
print();//Student
this.print();//Student
super.print();//Person
}
public void test(String name){
System.out.println(name);//jibu
System.out.println(this.name);//李骏
System.out.println(super.name);//棘布
}
}
package com.oop;
import com.oop.demo05.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
// student.test("jibu");
// student.test1();
}
}
* */
super注意点: 1.supper调用父类的构造方法,必须在构造方法的第一个 2.super必须只能出现在子类的方法或者构造方法中 3.super和this不能同时调用构造方法 VS this: 代表的对象不同: this:本身调用者这个对象 super:代表父类对象的引用 前提 this:没有继承也可以使用 super:只能在继承条件下才可以使用 构造方法 this(); 本类的构造 super(); 父类的构造
方法重写:
package com.oop.demo05;
//重写都是方法的重写,和属性无关
public class B {
public void test(){
System.out.println("B=>test()");
}
}
/*
package com.oop.demo05;
//继承
public class A extends B{
//Override 重写
@Override // 注解,有功能的注释
public void test() {
System.out.println("A>=test()");
}
}
/
package com.oop;
import com.oop.demo05.A;
import com.oop.demo05.B;
public class Application {
//静态的方法和非静态的方法区别很大!
//静态方法:加了static的静态方法,方法的调用只和左边,定义的数据类型有关
//非静态方法:重写
public static void main(String[] args) {
//有static时,b调用了B类的方法,因为b是B类的定义
//没有static时,b调用的是对象方法,而b是用A类new的
A a = new A();
a.test();//A
//父类的引用指向了子类
B b = new A();//子类重写了父类的方法
b.test();//B
}
}
* */
重写:需要有继承关系,子类重写父类的方法! 1.方法名必须相同 2.参数列表必须相同 3.修饰符:范围可以扩大: public > protected > default > private 4.抛出的异常:范围可以被缩小,但不能扩大 ClassNotFoundException --> Exception(大) 重写,子类的方法和父类必须要一致:方法体不同! 为什么需要重写: 1.父类的功能,子类不一定需要,或者不一定满足 2.重写是为了多态 快捷键: Alt + Insert : override