1>第一个题目
public class TestPerson
{
public static void main(String args[]){
Person p1 = new Person("李三",21,"编程");
Person p2 = new Person("张五",22,"java");
p1.getInfo();
p2.getInfo();
}
}
class Person
{
private String name; //姓名
private int age; //年龄
private String like; //爱好
public Person(){} //无参构造方法
public Person(String name,int age,String like){
this.name = name;
this.age = age;
this.like = like;
}
//取得姓名
public String getName(){
return name;
}
//取得年龄
public int getAge(){
return age;
}
//取得爱好
public String getLike(){
return like;
}
public void getInfo(){
System.out.println("姓名:"+name+"\t年龄:"+age+"\t爱好:"+like);
}
}
2>第二个题目
多态的两种表现形式:方法的重载和方法的重写
|