2021-01-16

类的封装与继承实例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,结果显示
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210116203238896.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5Mzg1,size_16,color_FFFFFF,t_70#pic_center)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值