java语言课程实验
- 提问者网友:缘字诀
- 2021-05-03 05:50
- 五星知识达人网友:逐風
- 2021-05-03 06:04
package javaee.xmal;
public class Pupil {
private String name; //姓名
private String stuNo; //学好
private double mark; //得分
public Pupil(){}
public Pupil(String name, String stuNo, double mark) {
super();
this.name = name;
this.stuNo = stuNo;
this.mark = mark;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public double getMark() {
return mark;
}
public void setMark(double mark) {
this.mark = mark;
}
public void introduce(){
System.out.println("姓名:"+this.getName()+"\t"+"学号:"+this.getStuNo()+"分数"+this.getMark());
}
public void myMethod(){
System.out.println("小学生类");
}
}
package javaee.xmal;
public class MiddleStudent1 extends Pupil {
private String mp3;
public MiddleStudent1() {
super();
}
public MiddleStudent1(String name,String stuNo,double mark,String mp3) {
super(name,stuNo,mark); //调用父类代餐构造
this.mp3 = mp3;
}
public String getMp3() {
return mp3;
}
public void setMp3(String mp3) {
this.mp3 = mp3;
}
public void introduce (){
System.out.println("姓名:"+this.getName()+"\t"+"学号:"+this.getStuNo()+"分数"+this.getMark()+"Mp3:"+this.getMp3());
}
public void myMethod(){
System.out.println("MiddleStudent1类");
}
}
package javaee.xmal;
public class MiddleStudent2 extends Pupil {
private String mobile;
public MiddleStudent2() {
super();
}
public MiddleStudent2(String mobile) {
super();
this.mobile = mobile;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public void introduce(){
System.out.println("姓名:"+this.getName()+"\t"+"学号:"+this.getStuNo()+"手机:"+ this.getMobile());
}
public void run(){
System.out.println("这是MiddleStudent2类自己的方法");
}
public void myMethod(){
System.out.println("MiddleStudent2类");
}
}
package javaee.xmal;
public class Student {
private Pupil pupil;
private String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Pupil getPupil() {
return pupil;
}
public void setPupil(Pupil pupil) {
this.pupil = pupil;
}
}
package javaee.xmal;
//实现多态
public class PolymiorphismImp {
public void getMyMethod(Pupil pupil){
pupil.myMethod();
pupil.introduce();
}
}
package javaee.xmal;
public class Test {
public static void main(String[] args) {
Pupil pubil = new Pupil("郑三","1001",76);
Pupil middleStudent1 = new MiddleStudent1("李思","1003",98,"mp3");
Pupil middleStudent2 = new MiddleStudent2("三星");
PolymiorphismImp imp = new PolymiorphismImp();
((MiddleStudent2)middleStudent2).run();
imp.getMyMethod(pubil);
imp.getMyMethod(middleStudent1);
imp.getMyMethod(middleStudent1);
System.out.println();
Student student = new Student();
System.out.println("这是内部类的测试");
student.setPupil(pubil);
System.out.println(student.getPupil().getName());
}
}
- 1楼网友:北城痞子
- 2021-05-03 07:12
//接口类输出“都是学生”
public interface DouShixuesheng{ public void shuohua(); }
//封装小学生类
class XiaoXuesheng implements DouShixuesheng
{
private int xuehao;
private float chengji;
private String name;
public void shuohua(){System.out.print("我是学生")}
public int getXuehao(){ return xuehao; } public void setXuehao(int xuehao){ this.xuehao= xuehao; } public float getChengji(){ return chengji; } public void setChengji(float Chengji){ this.chengji= chengji; }
public String getName()
{
return name;
}
public void setName(String name)
{
this.name= name;
}
}
//中学生继承小学生类extends
class ZhongXuesheng extends XiaoXuesheng
{
public void shuohua(){System.out.print("我是继承小学生类")}
}