java类——从子类调用父类方法:

2019年2月16日00:02:30 发表评论 931 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 super.talk()+"我在"+this.school + "上学";
        }
    }







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






我是:张三,今年:25岁我在北京上学
张三
25
北京

发表评论

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