创建继承于类person的类student
来源:互联网 发布:淘宝飞猪靠谱吗 编辑:程序博客网 时间:2024/05/22 20:28
package dan;class Person{//创建Person String name;//定义变量name String sex;//定义变量 int age;//定义变量 void getInfo(String n,String s,int a){//定义getInfo() name=n;//获取name的信息 sex=s;//获取sex的信息 age=a;//获取age的信息 } void showInfo(){//定义showInfo System.out.println("姓名:"+name);//输出name的信息 System.out.println("性别:"+sex); System.out.println("年龄:"+age); }}public class student extends Person{//定义子类Student继承父类Person,(若这里不用extends Person,下面name,sex,age将因找不到路径而显示错误,需重新定义) int id;//定义变量 String school; void setInfo(int num,String school_name){//定义setInfo() id=num;//将num赋值给id school=school_name; } void outputInfo(){//定义outputInfo System.out.println("学校:"+school);//显示school信息 System.out.println("学号:"+id); System.out.println("姓名:"+name); System.out.println("性别:"+sex); System.out.println("年龄:"+age); } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("第一个人的信息:"); student zhang=new student();//实例化 zhang.name="张力"; zhang.sex="男"; zhang.age=18; zhang.id=01; zhang.school="dsfgf"; zhang.showInfo();//调用父类的showInfo(),输出姓名性别年龄 System.out.println();//空行 zhang.outputInfo();//调用子类的outputInfo(),输出zhang的全部信息 System.out.println("第二个人的信息:"); student li=new student();//实例化 li.getInfo("沥沥","女",18); li.setInfo(02,"sfjn"); li.outputInfo();//调用outputInfo() }}
运行结果:
知识点:1.继承的实现
2.父类和子类
阅读全文
0 0
- 创建继承于类person的类student
- 创建继承于类person的类student
- 创建继承于类person的类student
- 创建继承类Person的类Student
- 创建继承类Person的类Student
- Java:创建继承于类Person的类Student并重写方法
- 创建继承于类person的类student并重写方法
- 创建子类student继承父类person
- 创建继承于父类Person的子类Studen
- 创建继承类Perint的类Student
- 9、 java Person,Student,GoodStudent 三个类的继承、构造函数的执行
- (继承)Problem A: Person类与Student类的关系
- Problem A: Person类与Student类的关系
- Problem A: Person类与Student类的关系
- Problem B: Person类与Student类的关系
- 《JAVA继承与多态》【Person、Student、Employee类】(注:此题在书上原题基础上有修改)设计一个名为Person的类和它的两个名为Student和Employee子类。
- Person Student Employee Faculty Staff 类
- 实现类Student的创建
- Jenkins On Kubernetes---Jenkins上Kubernetes Plugin的使用
- 剑指offer—替换空格
- 【菜鸟日记】不要62! HDU
- 使用response的writer(九)
- 奇妙的动态小程序
- 创建继承于类person的类student
- nyoj_58迷宫问题
- ChartDirectorPanel 图形展现组件
- 一维数组
- opencv for python (10) 图像旋转函数+仿射变换函数+透视变换
- XML、JSON及其解析
- Random的用法
- HTMLcanvas画矩形
- jQuery制作的简化三级联动菜单