第五周实验继承与多态(2)

2019-03-11 15:46

Java程序设计实验西南交大信息学院陈帆(2014) void check() { String s=null; if (userName!=null) s=\用户名:\ else s=\用户名不能为空!\ if (password!=\ s=s+\ 口令无效!\ else s=s+\ 口令:********\ System.out.println(s); } } public class D1 { public static void main(String[] args) { new RunDemo(); new RunDemo(\ new RunDemo(null,\ new RunDemo(\ } } 要求:给出程序的运行结果 _______________________________________________________________ 【实验结果与分析】 04、编程,设计一个人类Human,有属性姓名name、性别sex、年龄age和身高height,有思考行为think;设计一个继承人类的学生类Pupil,有属性学号stuNo,有学习行为learn;再设计一个继承学生类的大学生类Student,有自学行为study;最后设计一个入口类TestStudent,构造若干个大学生,并把大学生们的属性和行为一一列举出来。 提示:运行结果参考如下。 第6页共13页

Java程序设计实验西南交大信息学院陈帆(2014) 图 运行界面 部分程序参考如下。 class Human { //人类 String name; char sex; ... public void think(){ System.out.println(this.name + \在思考……\ } Human() {} Human(String n, char s, int a, double h) { name = n; sex = s; ... } } class Pupil extends Human { //学生类 String stuNo; public void learn() { System.out.println(this.name + \在学习……\ } Pupil () {} Pupil (String no, String n, char s, int a, double h) { super(n, s, a, h); ... } } class Student extends Pupil { //大学生类 public void study() { System.out.println(this.name + \在自学……\ } Student() {} Student (String no, String n, char s, int a, double h) { ... } } public class TestStudent { //入口类 public static void main(String args[]) { Student s1 = new Student(\张三\男', 18, 170); System.out.println(s1.name + \,学号\,\,\岁,身高第7页共13页

Java程序设计实验西南交大信息学院陈帆(2014) \s1.think() ; ... } } 要求: (1)给出完整的程序,并写出运行结果(该题要求:运行结果用截图) ___________________________________________________________________________________ 【实验结果与分析】 完整程序与运行结果: package text2; class Human{ } class Pupil extends Human{ String stuNo; public void learn(){ 第8页共13页

String name; char sex; int age; double height; public void think(){ } Human(){} Human(String n,char s,int a,double h){ } name=n; sex=s; age=a; height=h; System.out.println(this.name+\在思考??\Java程序设计实验西南交大信息学院陈帆(2014) } class Student extends Pupil{ } public class TestStudent { public static void main(String args[]){ Student s1=new Student(\张三\男',18,170); System.out.println(s1.name+\。学号\,\,\岁,身高\s1.think(); s1.learn(); s1.study(); Student s2=new Student(\李四\女',17,150); System.out.println(s2.name+\。学号\,\,\岁,身高\s2.think(); s2.learn(); s2.study(); 第9页共13页

} System.out.println(this.name+\在学习??\Pupil(){} Pupil(String no,String n,char s,int a,double h){ } super(n,s,a,h); stuNo=no; public void study(){ } Student(){} Student(String no,String n,char s,int a,double h){ } super(no,n,s,a,h); System.out.println(this.name+\在自学??\Java程序设计实验西南交大信息学院陈帆(2014) } 实验结果: } ______________________________________________________________________________________________ 05、在一个类中编写4个两数相加的重载方法,参数分别为整整,整实、实整、实实。并在另一个类中编程测试这些方法的运行结果。 提示:运行结果参考如下。 图 运行界面 部分程序参考如下: class Operate{ static int add(int i, int j){ return (i+j); } static double add(int i, double d){ ... } ... } public class TestAdd{ public static void main(String [] args) { System.out.println(1+\System.out.println(2 +\... } } 第10页共13页


第五周实验继承与多态(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:投标毕业设计说明

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: