在java 编写一个person 类,有三个属性姓名,性别,年龄,并有一个introduc 方法,可以输出“我是张某某,男,16”,接着写一个person的子类student,加上属性班级,并重写introduc方法,可以输出“我是学生张某某,来自某某班,男,16”。
时间: 2024-04-30 12:18:39 浏览: 104
以下是Java代码:
```java
public class Person {
private String name;
private String gender;
private int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public void introduce() {
System.out.println("我是" + name + "," + gender + "," + age);
}
}
public class Student extends Person {
private String className;
public Student(String name, String gender, int age, String className) {
super(name, gender, age);
this.className = className;
}
@Override
public void introduce() {
System.out.println("我是学生" + name + ",来自" + className + "," + gender + "," + age);
}
}
```
使用示例:
```java
public class Main {
public static void main(String[] args) {
Person p = new Person("张某某", "男", 16);
p.introduce(); // 输出:我是张某某,男,16
Student s = new Student("张某某", "男", 16, "某某班");
s.introduce(); // 输出:我是学生张某某,来自某某班,男,16
}
}
```
阅读全文