目录
- 继承
-
- 1.基本语法
- 2.使用继承时的注意事项
- 3.使用继承的目的
- 4.继承中的代码块
- 5.组合
继承
1.基本语法
使用 extends 指定父类。 is-a的关系。
class 子类 extends 父类 {
}
父类也称基类、超类。子类也成派生类。
2.使用继承时的注意事项
(1)子类继承了父类中除构造方法外的所有东西。
代码示例:
class Cat extends Animal{
private String sex;
public Cat(String name, int age) {
super(name, age);//父类中有name和age,cat已经继承,此处不用再写。
}
}
(2)super关键字的用法:
①super();调用父类的构造方法。(必须放在第一行)
②super.func(); 调用父类的方法。
③super.data; 调用父类的数据成员。
代码示例:
class Animal {
protected String name;
private int age;
public int a;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void sleep(){
System.out.println("睡觉");
}
}
class Cat extends Animal{
private String sex;
public Cat(String name, int age) {
super(name, age);
}
public void func(){
super.sleep();
int a=super.a;
}
}
(3)如果要构造子类,必须要先构造父类。
代码示例:
class Cat extends Animal{
private String sex;
public Cat(String name, int age) {
super(name, age); //用super关键字,必须放在第一行。
}
}
(4)父类只能访问自己的数据成员或者方法。
代码示例:
class Animal {
protected String name;
private int age;
public int a;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void sleep(){
System.out.println("睡觉");
}
}
class Cat extends Animal{
private String sex;
public Cat(String name, int age) {
super(name, age);
}
public void func(){
super.sleep();
int a=super.a;
}
}
public class Test {
public static void main(String[] args) {
Animal animal=new Animal("动物",12);
animal.sex;//此处报错 因为父类Animal中没有sex数据成员。
}
}
(5)Java 中一个子类只能继承一个父类。
(6)对于父类的 private 的字段和方法, 子类中是无法访问的。
(7)被final所修饰的类叫做密封类,不能被继承。
(8)B继承A,C继承B,这种称为多层继承。
3.使用继承的目的
为了代码的复用。 建立了类与类之间的关系,继承是多态的前提。
代码示例:
Animal是父类,Dog和Cat是子类。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLykzM0QTOyAjM1ETMxkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
class Animal {
protected String name;
private int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void eat(){
System.out.println(this.name+"吃饭");
}
}
class Cat extends Animal{
private String sex;
public Cat(String name, int age) {
super(name, age);
}
public Cat(String name, int age, String sex) {
super(name,age);
this.sex = sex;
}
}
class Dog extends Animal {
private String sex;
public Dog(String name, int age, String sex) {
super(name,age);
this.sex = sex;
}
}
public class Test {
public static void main(String[] args) {
Cat cat=new Cat("喵喵",2,"man");
cat.eat();
System.out.println("==================");
Dog dog=new Dog("叮当",3,"man");
dog.eat();
dog.bark();
}
}
4.继承中的代码块
先打印父类的静态代码块,在打印子类的静态代码块,然后为实例代码块,然后构造方法。
5.组合
a part of 或者has a的关系
代码示例:
class Address{
public String domic;
public int floor;
public Address(String domic, int floor) {
this.domic = domic;
this.floor = floor;
}
@Override
public String toString() {
return "Address{" +
"domic='" + domic + '\'' +
", floor=" + floor +
'}';
}
}
class Student{
public String name;
public int age;
public Address address;
public Student(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
}
public class Test2 {
public static void main(String[] args) {
Address address=new Address("#1",3);
Student student=new Student("柠萌",18,address);
System.out.println(student);
}
}
//打印结果:Student{name='柠萌', age=18, address=Address{domic='#1', floor=3}}