0
点赞
收藏
分享

微信扫一扫

InheritDemo


package com.shrimpking.t2;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/9/16 20:22
 */
class Person2{
    String name;
    int age;

    Person2(String name,int age){
        this.name = name;
        this.age = age;
    }

    void speak(){
        System.out.printf("我是%s,今年%d岁%n",this.name,this.age);
    }
}

class Student2 extends Person2{

    String school;

    Student2(String name, int age,String school)
    {
        //调用父类的构造
        super(name, age);
        this.school = school;
    }

    void study(){
        System.out.printf("我在%s读书",this.school);
    }
}

public class InheritDemo
{
    public static void main(String[] args)
    {
        Student2 stu = new Student2("zhang",25,"工业大学");
        stu.speak();
        stu.study();

    }
}

举报
0 条评论