首页 > 学院 > 开发设计 > 正文

super的使用

2019-11-06 08:08:19
字体:
来源:转载
供稿:网友

person,父类

public class Person { public Person(String name){ this.name=name; } PRotected String name; public void sayName(){ System.out.println("My name is "+this.name); } public void description(){ System.out.println("I am a human"); }}

student子类

public class Student extends Person{ public Student(String name){ super(name); } public void description(){ System.out.println("I am a student"); }}

调用

public class test { public static void main(String[] args) throws ParseException { Person person=null; Student student=null; student=new Student("jack"); person=(Person)student; person.sayName(); person.description(); }}

输出结果 My name is jack I am a student


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表