package com.itheima_01;
//学生类
public class Student extends Person {
/*private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}*/
public Student() {
}
public Student(String name, int age) {
super(name,age);
}
public void study(){
System.out.println("读书成才。。。");
}
}
//老师类
package com.itheima_01;
public class Teacher extends Person {
/*private String name;
private int age;
public Teacher() {
}
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}*/
public Teacher() {
}
public Teacher(String name, int age) {
super(name,age);
}
public void teach(){
System.out.println("教书育人。。。");
}
}
//人类
package com.itheima_01;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//测试类
package com.itheima_01;
public class Demo1 {
public static void main(String[] args) {
Teacher t1 = new Teacher();
t1.setName("王老师");
t1.setAge(20);
System.out.println(t1.getName() + ", " + t1.getAge());
t1.teach();
Teacher t2 = new Teacher("李老师", 18);
System.out.println(t2.getName() + ", " + t2.getAge());
t2.teach();
Student s1 = new Student();
s1.setName("张三");
s1.setAge(16);
System.out.println(s1.getName() + ", " + s1.getAge());
s1.study();
Student s2 = new Student("李四", 17);
System.out.println(s2.getName() + ", " + s2.getAge());
s2.study();
}
}
//猫类
package com.itheima_02;
public class Cat extends Animals {
public Cat() {
}
public Cat(String name, int age, String color) {
super(name, age, color);
}
public void catchMouse(){
System.out.println("抓老鼠...");
}
public String toString() {
return "Cat{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", color='" + getColor() + '\'' +
'}';
}
@Override
public void eat() {
System.out.println("吃鱼...");
}
}
//狗类
package com.itheima_02;
public class Dog extends Animals {
private int truly;
public Dog() {
}
public Dog(String name, int age, String color, int truly) {
super(name, age, color);
this.truly = truly;
}
public int getTruly() {
return truly;
}
public void setTruly(int truly) {
this.truly = truly;
}
public void lookHome(){
System.out.println("看家...");
}
@Override
public String toString() {
return "Dog{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", color='" + getColor() + '\'' +
", truly=" + getTruly() +
'}';
}
@Override
public void eat() {
System.out.println("吃骨头...");
}
}
//动物类
package com.itheima_02;
/*
继承和重写结合案例
*/
public class Animals {
private String name;
private int age;
private String color;
public Animals() {
}
public Animals(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void eat(){
System.out.println("吃饭...");
}
@Override
public String toString() {
return "Animals{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
}
//测试类
package com.itheima_02;
public class Demo1 {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.setName("二哈");
d1.setAge(2);
d1.setColor("黑白色");
d1.setTruly(-100);
System.out.println(d1);
d1.eat();
d1.lookHome();
System.out.println("============");
Dog d2 = new Dog("中华田园犬",3,"黄色",100);
System.out.println(d2);
d2.eat();
d2.lookHome();
System.out.println("============");
Cat c1 = new Cat();
c1.setName("黄猫");
c1.setAge(3);
c1.setColor("黄色");
System.out.println(c1);
c1.eat();
c1.catchMouse();
System.out.println("============");
Cat c2 = new Cat("布偶猫",2,"灰白色");
System.out.println(c2);
c2.eat();
}
}
修饰符 | 同一个类中 | 同一个包中子类无关类 | 不同包的子类 | 不同包的无关类 |
| private | √ | |||
| 默认 | √ | √ | ||
| protected | √ | √ | √ | |
| public | √ | √ | √ | √ |
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |