day09--继承的入门

This commit is contained in:
2026-01-05 11:36:00 +08:00
parent 19be654735
commit 92db74e2fb
6 changed files with 75 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
package com.inmind.extend01;
public class Assistant extends Employee{
//特有行为
public void help(){
System.out.println("年龄为"+age+",姓名为"+name+"帮助学生解决学习和生活上的问题");
}
}

View File

@@ -0,0 +1,9 @@
package com.inmind.extend01;
public class BanZhuRen extends Employee{
//特有行为
public void manage(){
System.out.println("管理班级");
}
}

View File

@@ -0,0 +1,29 @@
package com.inmind.extend01;
/*
继承:对同一类事物的相同的属性和行为,进行“共性抽取”
继承的格式和和基本使用
public class Fu{
}
public class Zi extends Fu{
}
继承的特点子类拥有父类的所有内容除了被private修饰的和构造方法。
继承的好处:实现多个类中相同内容的复用,子类可以实现各自的不同功能需求
*/
public class Demo01 {
public static void main(String[] args) {
//创建一个老师对象
Teacher teacher = new Teacher();
teacher.name = "张三";
teacher.age = 30;
teacher.teach();
//创建一个辅导员
Assistant assistant = new Assistant();
assistant.name = "李四";
assistant.age = 32;
assistant.help();
}
}

View File

@@ -0,0 +1,7 @@
package com.inmind.extend01;
public class Employee {
//属性
String name;
int age;
}

View File

@@ -0,0 +1,8 @@
package com.inmind.extend01;
public class Teacher extends Employee {
//特有行为
public void teach(){
System.out.println("年龄为"+age+",姓名为"+name+"老师来教学");
}
}

View File

@@ -0,0 +1,14 @@
package com.inmind.extend01.test;
/*
请大家使用继承描述一下,动物类,狗类,猫类,相同的
属性name age color
行为eat
特有的行为:
狗看门
猫抓老鼠
创建猫和狗对象,设置属性,并调用各自的方法
*/
public class Demo02 {
}