博客
关于我
继承案例分析一(学生类)
阅读量:339 次
发布时间:2019-03-04

本文共 1797 字,大约阅读时间需要 5 分钟。

Java继承关系的案例分析

通过设计和实现简单的Java类,能够帮助开发者巩固继承关系的概念。对于编程而言,基本的Java类是构建应用程序的基石,也是理解各种概念的最佳载体。

案例一:Person类与Student类的设计

目标:

  • 创建一个Person类,包含姓名、地址、性别和年龄这四个私有属性。
  • 为该类设计四种不同的构造方法:
    • 四参数构造方法(接受姓名、地址、性别和年龄)。
    • 两参数构造方法(接受姓名和地址,默认性别为“男”,年龄为0)。
    • 无参数构造方法。
  • 增加一个getInfo()方法,用于返回四个属性的值。
  • 实现:

    class Person {    private String name; // 姓名    private String addr; // 地址    private char sex;  // 性别    private int age;   // 年龄    public Person() { } // 无参数构造方法    public Person(String name, String addr) {        this(name, addr, '男', 0);    }    public Person(String name, String addr, char sex, int age) {        this.name = name;        this.addr = addr;        this.sex = sex;        this.age = age;    }    public String getInfo() {        return "姓名:" + this.name + "\t地址:" + this.addr + "\t性别:" + this.sex + "\t年龄:" + this.age;    }}

    扩展:Student类

    Student类继承自Person类,增加两个私有属性mathenglish来存储数学和英语成绩。

  • 设计三种构造方法:
    • 六参数构造方法(接受姓名、地址、性别、年龄、数学成绩和英语成绩)。
    • 两参数构造方法(接受姓名和地址,其他属性默认值)。
    • 无参数构造方法。
  • 重写getInfo()方法,返回学生的全部属性信息。
  • 实现:

    class Student extends Person {    private double math; // 数学成绩    private double english; // 英语成绩    public Student() { } // 无参数构造方法    public Student(String name, String addr) {        super(name, addr);    }    public Student(String name, String addr, char sex, int age, double math, double english) {        super(name, addr, sex, age);        this.math = math;        this.english = english;    }    public String getInfo() {        return super.getInfo() + "数学成绩:" + this.math + "\t英语成绩:" + this.english;    }}

    使用示例:

    public class JavaDemo {    public static void main(String[] args) {        Student stuA = new Student(12.3, 99.2, "张三", "湖南", '男', 18);        System.out.println(stuA.getInfo());    }}

    总结

    通过上述案例,可以清晰地看到Java继承关系的实际应用。Student类继承Person类,继承了所有基本属性和方法,同时新增了特有的属性和方法。这是Java面向对象编程的核心概念,也是理解更复杂继承关系的基础。

    转载地址:http://aqwe.baihongyu.com/

    你可能感兴趣的文章
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
    查看>>
    org.springframework.beans.factory.BeanDefinitionStoreException
    查看>>
    org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>
    SQL-CLR 类型映射 (LINQ to SQL)
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>