JAVA简单抽象实现方法
程序员文章站
2022-07-05 23:43:18
...
public abstract class Person {
private String name;
public Person(String name) {
this.name=name;
}
public abstract String desption();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/////////////
public class Student extends Person {
public Student(int id,String name,int age,Date birth){
super(name);
this.id=id;
this.age=age;
this.birth=birth;
}
@Override
public String desption() {
return ("这是学生的信息 "+age);
}
private int id;
private int age;
private Date birth;
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/////
public class Employee extends Person {
Employee(String str,String name,Double dou)
{
super(name);
this.str=str;
this.number=dou;
}
@Override
public String desption() {
return ("是这是雇员的信息 "+str);
}
private String str;
private Double number;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public Double getNumber() {
return number;
}
public void setNumber(Double number) {
this.number = number;
}
}
/////
public class PersonTest {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
SimpleDateFormat df=new SimpleDateFormat("yyy-mm-dd");
Person[] person =new Person[2]; //可以创建对象数组,不能直接创建对象
person[0]=new Student(123,"学生姓名",18,df.parse("1988-07-20"));
person[1]=new Employee("其它参数","雇员名字",12354.25622);
for(Person p:person){
System.out.println("这是抽象类的方法 "+p.desption());
System.out.println("抽象方法的类名 "+p.getName());
}
Person person2=new Student(789,"学名",22,df.parse("1998-08-88"));
System.out.println("单个方法 "+person2.desption());
System.out.println("单个姓名 "+person2.getName());
System.out.println(((Student) person2).getAge());
}
}
/*
实现类后,要调用子方法必须要对象转化,貌似没有接品完美
抽象类的实现方法,只能创建他的数组对象,不能实例化
用抽象类去创建继承类,可以直接创建对象,跟接口一样,但不知道与接口的好处是否一样,期待高手解答
*/
private String name;
public Person(String name) {
this.name=name;
}
public abstract String desption();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/////////////
public class Student extends Person {
public Student(int id,String name,int age,Date birth){
super(name);
this.id=id;
this.age=age;
this.birth=birth;
}
@Override
public String desption() {
return ("这是学生的信息 "+age);
}
private int id;
private int age;
private Date birth;
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/////
public class Employee extends Person {
Employee(String str,String name,Double dou)
{
super(name);
this.str=str;
this.number=dou;
}
@Override
public String desption() {
return ("是这是雇员的信息 "+str);
}
private String str;
private Double number;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public Double getNumber() {
return number;
}
public void setNumber(Double number) {
this.number = number;
}
}
/////
public class PersonTest {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
SimpleDateFormat df=new SimpleDateFormat("yyy-mm-dd");
Person[] person =new Person[2]; //可以创建对象数组,不能直接创建对象
person[0]=new Student(123,"学生姓名",18,df.parse("1988-07-20"));
person[1]=new Employee("其它参数","雇员名字",12354.25622);
for(Person p:person){
System.out.println("这是抽象类的方法 "+p.desption());
System.out.println("抽象方法的类名 "+p.getName());
}
Person person2=new Student(789,"学名",22,df.parse("1998-08-88"));
System.out.println("单个方法 "+person2.desption());
System.out.println("单个姓名 "+person2.getName());
System.out.println(((Student) person2).getAge());
}
}
/*
实现类后,要调用子方法必须要对象转化,貌似没有接品完美
抽象类的实现方法,只能创建他的数组对象,不能实例化
用抽象类去创建继承类,可以直接创建对象,跟接口一样,但不知道与接口的好处是否一样,期待高手解答
*/
上一篇: JS 表单验证的15个正则表达式
下一篇: Random随机生成ID