实验五 继承与接口(一)

1、父类与子类

class Point{
	private double x;
	private double y;
	
	Point(double x,double y){
		this.x=x;
		this.y=y;
	}
	
	void print(){
		System.out.printf("x=%4.2f,y=%4.2f\n",x,y);
	}
}
class ColorPoint extends Point{
	int colorType;
	final static char[] colors={'R','G','B'};
	ColorPoint(double x,double y,int colorType){
		super(x,y);
		this.colorType=colorType;
	}
	void print(){
		super.print();
		System.out.printf("color:%s",colors[colorType]);
	}
	public static void main(String args[]){
		ColorPoint cp=new ColorPoint(8,7,1);
		cp.print();
	}
}


2、父类与成员变量的访问

class Person{
	private String idNo;
	String name;
	boolean sex;
	int age;

	public Person(String idNo,String name,boolean sex,int age){
		this.idNo=idNo;
		this.name=name;
		this.sex=sex;
		this.age=age;
	}
	public void display(){
		System.out.println("姓名:  "+name);
		System.out.println("性别:  "+(sex?"男":"女"));
		System.out.println("年龄:  "+age);
		System.out.println("身份证:"+idNo);
	}
}
class Student extends Person{
	String studentNo;
	public Student(String studentNo,String idNo,String name,boolean sex,int age){
		super(idNo,name,sex,age);
		this.studentNo=studentNo;
	}
	public void display(){
		System.out.printf("学号:  %s\n",studentNo);
		super.display();
	}
}

class PersonTest{
	public static void main(String args[]){
		Student st=new Student("11201501","362326","lijiaying",false,20);
		st.display();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值