java类——用this调用和用super调用父类参数和方法的区别:

2019年2月15日23:50:46 发表评论 993 views
/**
 * Created by xabcd on 2019/2/15.
 */


    class Person8
    {
        String name;
        int age;
        public String talk(){
            return "我是:"+this.name+",今年:"+this.age+"岁";
        }
    }
    class Studen extends Person8
    {
        String school;
        public Studen(String name,int age,String school)
        {
            //分别为属性赋值
            this.name = name;
            this.age = age;
            this.school = school;
        }
        //此处覆写Person8中的talk()方法
        public String talk()
        {
            return "我在"+this.school + "上学";
        }
    }


/**
 * Created by xabcd on 2019/2/15.
 */
public class TestPerson8
    {
        public static void main(String args[]){
        Studen ss = new Studen("张三",25,"北京");
        System.out.println(ss.talk());
System.out.println(ss.talk());
System.out.println(ss.name);
System.out.println(ss.age);
System.out.println(ss.school);
} } 



结果: 我在北京上学
张三
25
北京






第 34 行 调用 talk() 方法 实际 上调 用的 只是 子类 的 方法, 那 如果 现在 需要 调用 父 类 中的 方法 该 如何 实现 呢? 请看 下面 的 范例, 此 范例 修改 自上 一个 范例。

 


			

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: