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();
}
}