类的封装与继承实例2
1,简单的类的继承问题
首先编写父类People,属性有name和age
行为有吃和说
子类学生Student,属性有Sno,行为有学习
2,难点在于子类继承时,super(name,age)的理解
3,父类People代码
```java
import java.util.jar.Attributes.Name;
/*类的继承
* 1,People为父类,属性值为姓名和年龄,行为有吃和说
* 2,Student为子类,属性有学号,行为有学习
* 3,在入口函数中对类进行实例化
*
*/
public class People {
private String name ;
private int 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 People(String name, int age) {
super();
this.name = name;
this.age = age;
}
public People(){}
public static void eat() {
System.out.println("吃饭饭");
}
public static void speak() {
System.out.println("说话话");
}
}
子类student
```java
public class Student extends People{
private String Sno;
public String getSno() {
return Sno;
}
public void setSno(String sno) {
Sno = sno;
}
public Student(String name, int age, String sno) {
super(name, age);
this.Sno = sno;
}
public static void learn() {
System.out.println("努力学习哦");
}
public static void main(String[] args) {
Student S1=new Student("陈峰",34,"2018303886");
System.out.println("姓名:"+S1.getName());
System.out.println("年龄:"+S1.getAge());
System.out.println("学号:"+S1.getSno());
}
}
4,结果显示


1万+

被折叠的 条评论
为什么被折叠?



