创建继承于类person的类student并重写方法
来源:互联网 发布:优化游戏的软件哪个好 编辑:程序博客网 时间:2024/05/22 00:41
package hop;class person{ //创建父类person String name; String sex; int age; //定义父类中的变量 void getInfo(String n,String s,int a)//定义方法getInfo(),获取个人信息 { name=n; sex=s; age=a; } //定义方法getInfo(),获取个人信息 void showInfo()//定义方法showInfo(),显示个人信息 { System.out.println("姓名:"+name); System.out.println("性别:"+sex); System.out.println("年龄:"+age); }//定义方法showInfo(),显示个人信息}public class student extends person{ //创建子类student来继承父类person String id; //定义变量 String school; //定义子类中的变量 void setInfo(String num,String school_name) { id=num; school=school_name; //定义方法geeInfo(),获取学生的学校名称和学号 } void outputInfo() { System.out.println("学校:"+school); System.out.println("学号:"+id); System.out.println("姓名:"+name); System.out.println("性别:"+sex); System.out.println("年龄:"+age); //定义方法outputInfo(),显示学生信息 } public static void main(String[] args) { // TODO 自动生成的方法存根 System.out.println("第一个人的信息"); student jone=new student(); jone.name="花花"; //调用方法,输入第一个学生的信息 jone.sex="男"; jone.age=18; jone.id="05"; jone.school="SISO"; jone.showInfo(); System.out.println("第二个人得信息"); student Mica=new student(); Mica.getInfo("雨雨", "女", 20);//调用方法,输入第二个学生的信息 Mica.setInfo("10","SISO"); Mica.outputInfo(); //创建属于学生类的对象,并通过对象调用其相关的成员变量和成员方法 }}
知识要点:
.方法重写
.成员变量的隐藏
.super关键字
程序分析:
此程序定义一个类Person,在该类中包含name,sex,age3个属性以及getInfo()、showInfo()两个方法。同时创建一个类Student,该类继承自类Person(即Person为父类)。类Student除了具有父类Person的相关属性和方法之类,也具有属于自己的属性school和id,以及方法setInfo()和outputInfo()。定义类之后,可以创建属于类的对象,这里创建了类Student的对象jone和Mica。由于对象属于类Student,所以这些对象除了可以调用类Student的属性和方法(如jone.school,Mica.setInfo())之外,也可以调用属于类Person的属性和方法(如jone.name,Mica.getInfo())。
阅读全文
0 0
- Java:创建继承于类Person的类Student并重写方法
- 创建继承于类person的类student并重写方法
- 创建继承于类person的类student
- 创建继承于类person的类student
- 创建继承于类person的类student
- 创建继承类Person的类Student
- 创建继承类Person的类Student
- 创建子类student继承父类person
- 创建继承于父类Person的子类Studen
- java:继承并重写的方法
- 创建继承类Perint的类Student
- 9、 java Person,Student,GoodStudent 三个类的继承、构造函数的执行
- (继承)Problem A: Person类与Student类的关系
- 4、创建产品分类的业务类并重写删除方法
- 子类继承父类,并重写父类方法
- Problem A: Person类与Student类的关系
- Problem A: Person类与Student类的关系
- Problem B: Person类与Student类的关系
- luogu P1443 马的遍历 【队列 dfs】
- ftp文件传输协议
- SpringBoot上传文件
- (转)姚期智:呼之欲出的量子计算机,和它漫长的最后一英里(全文)
- Android网络编程
- 创建继承于类person的类student并重写方法
- linux—c结构体学习
- 处理输入成绩异常
- 线程基础(二):线程锁和条件变量(笔记)
- Hibernate的查询方式
- windows中AD域的部署
- 模型的评估方法
- 单行文本过长自动替换为省略号的css写法:text-overflow:ellipsis;
- Tomcat安装及配置教程