1.3.2 继承内容:子类继承父类所有 public 和 protected 修饰的属性和方法,private 修饰的不能继承
1.3.3 示例:
class Animal { public int age; public String type; public void eat() { System.out.println("吸收营养"); } public void move() { System.out.println("会移动"); } } class Cat extends Animal { // 自动继承 age、type、eat()、move() } class Dog extends Animal { // 自动继承 age、type、eat()、move() }
1.3.4 调用继承的方法:子类对象可以直接调用父类的非私有方法
Cat cat = new Cat(); cat.move(); // 继承自 Animal Dog dog = new Dog(); dog.eat(); // 继承自 Animal
2. 示例代码
package com.itszt.day04;public class Demo07 { public static void main(String[] args) { Cat cat = new Cat(); cat.move(); // 继承自 Animal:会移动 Dog dog = new Dog(); dog.eat(); // 继承自 Animal:吸收营养 }}class Animal { public int age; public String type; public void eat() { System.out.println("吸收营养"); } public void move() { System.out.println("会移动"); }}class Cat extends Animal {}class Dog extends Animal {}