Java基础入门笔记-重写

首先是HelloWorld.java

package my;

public class HelloWorld
{

	public static void main(String[] args)
	{		
		Student s1=new Student();
		s1.id="20180103";
		s1.name="张三";
		s1.sex=true;
		s1.cellphone="18888888888";
		s1.weight=100;
		s1.height=170;
		s1.showHeightAndWeight();
	}

}

Man.java

package my;

public class Man
{
	public int height;
	public int weight;
	
	public void showHeightAndWeight() {
		System.out.println("height is"+height+", weight is"+weight);
	}
}

Student.java

package my;

public class Student extends Man
{
	public String id;
	public String name;
	public boolean sex;
	public String cellphone;
	public StudentCards cards=new StudentCards();
	@Override
	public void showHeightAndWeight()
	{
		super.showHeightAndWeight();
		System.out.println(id+name+sex+cellphone);
	}
	
	
}

StudentCards.java

package my;

public class StudentCards
{
	private String SchoolCardNum;
	private String BankCardNum;
	public String getSchoolCardNum()
	{
		return SchoolCardNum;
	}
	public void setSchoolCardNum(String schoolCardNum)
	{
		SchoolCardNum = schoolCardNum;
	}
	public String getBankCardNum()
	{
		return BankCardNum;
	}
	public void setBankCardNum(String bankCardNum)
	{
		BankCardNum = bankCardNum;
	}
	
}


如何用eclipse重写,如下图所示:




运行结果如下:


@Override这行也是有用的,不要删掉。

相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页