Java面向对象思想:关于继承的理解(通过代码加深)

继承:英文名:inheritance。是面向对象编程的核心机制之一,其本质是在已有基础之上进行的扩充和改造。关键字是:extends。

在Java语言中只能支持单继承,不允许多继承。

(1)一个子类只有一个父类

(2)一个父类可以派生出多个子类

核心的概念:方法重写:overwrite,在子类中根据需要对父类中继承的方法进行重新定义。

以下是继承相关代码的简单演示:

public class  Person
{
    private String name;
    private int age;

    public Person()
    {
    }

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

    //set和get方法
    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public int getAge()
    {
        return age;
    }


    public String getInfo()
    {
        return "Name:" + name + "\nAge:" + age;
    }

 

public class  Student extends Person
{
    public Student()
    {
    }

    private String school;

    public void setSchool(String school)
    {
        this.school = school;
    }

    public String getSchool()
    {
        return school;
    }

    public String getInfo()//方法的重写
    {
        
        return super.getInfo() + "\nSchool:" + school;
    
    }
    
}
 

public class TestStudent 
{
    public static void main(String[] args) 
    {
        Student stu = new Student();

        stu.setName("张三");
        stu.setAge(30);
        stu.setSchool("qqhru");
        System.out.println(stu.getInfo());
        
    }
}
 

 

长方形和正方形的案例:

public class  Rectangle
{
    protected double length;
    protected double width;

    public Rectangle()
    {
    }

    public Rectangle(double length, double width)
    {
        this.length = length;
        this.width = width;
    }

    public double calcPerimeter()
    {
        return 2 * (length + width);
    }

    public double calcArea()//面积保留三位,第四位四舍五入
    {
        return (int)((length * width) * 1000 + 0.5) / 1000.0;
    }

    public String toString()
    {
        return "长方形的长:" + length + ",宽:" + width;
    
    }

    
}


 

public class Square extends Rectangle 
{
    public Square(double side)        
    {
        super(side,side);    //调用父类的构造方法
    }

    public double calcPerimeter()
    {
        return super.calcPerimeter();
    }

    public double calcArea()
    {
        return super.calcArea();
    
    }

    public String toString()
    {
        return "正方形的边长:" + length;
    
    }
}
 

public class TestShape 
{
    public static void main(String[] args) 
    {
        Rectangle rectangle = new Rectangle(15.82,10.73);

        Square square = new Square(7.75);

        System.out.println(rectangle + ",其周长:" + rectangle.calcPerimeter() + ",其面积:" + rectangle.calcArea());

        System.out.println(square + ",其周长:" + square.calcPerimeter() + ",其面积:" + square.calcArea());

    }
}
 

测试结果截图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值