用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

父类与子类的声明应用

2021-11-05 作者: GuanTou举报

[java]代码库

package c;
import java.util.*;
public class my {
	public static void main(String[] args) {
        Student c1=new Student();
        Student c2=new Student();
        c1.setnumber(2019110105);
        c1.setname("chen");
        c1.setEgrade(87);
        c1.setMgrade(78);
        c1.setCgrade(84);
        c1.Sum();
        c1.equal();
        System.out.println("总成绩为:"+c1.getSumgrade());
        System.out.println("平均成绩为:"+c1.getequalgrade());
        System.out.println(c1.toString());
        c2.Student(2019110104, "fang", 70, 78, 94);
        c2.Sum();
        c2.equal();
        System.out.println(c2.toString());
        c1.Compare(c2);
    }
}
 class Student {
    public int number;
    public double Egrade,Mgrade,Cgrade,Sumgrade,equalgrade,testscore;
    public String name;
    public Student() {   
    }
    public void Student(int number,String name,double Egrade,double Mgrade,double Cgrade) {//构造方法
        this.number=number;
        this.name=name;
        this.Egrade=Egrade;
        this.Cgrade=Cgrade;
        this.Mgrade=Mgrade;
    }
    public int getnumber() {//get方法
        return number;
    }
    public String getname() {
        return name;
    }
    public double getEgrade() {
        return Egrade;
    }
    public double getMgrade() {
        return Mgrade;
    }
    public double getCgrade() {
        return Cgrade;
    }
    public double getSumgrade() {
        return Sumgrade;
    }
    public double getequalgrade() {
        return equalgrade;
    }
    public void setnumber(int number) {//set方法
        this.number=number;
    }
    public void setname(String name) {
        this.name=name;
    }
    public void setEgrade(double Egrade) {
        this.Egrade=Egrade;
    }
    public void setMgrade(double Mgrade) {
        this.Mgrade=Mgrade;
    }
    public void setCgrade(double Cgrade) {
        this.Cgrade=Cgrade;
    }
    public void setSumgrade(double Sumgrade) {
        this.Sumgrade=Sumgrade;
    }
    public void setequalgrade(double equalgrade) {
        this.equalgrade=equalgrade;
    }
    public void Sum() {//sum方法
        Sumgrade=Egrade+Mgrade+Cgrade;
    }
    public void equal() {//equal方法
        equalgrade=Sumgrade/3;
    }
    public void Compare(Student p) {//compare方法
        if(this.Sumgrade>p.Sumgrade) {
            System.out.println(this.name+"同学成绩高");
        }
        else if(this.Sumgrade==p.Sumgrade) {
            System.out.println("两位同学成绩相同");
        }
        else
            System.out.println(p.name+"同学成绩高");
    }
    public void testScore() {
        testscore=Sumgrade/3;
    }
    public String toString() {//toString方法
        return "Student [number=" + number + ", Egrade=" + Egrade + ", Mgrade=" + Mgrade + ", Cgrade=" + Cgrade
                + ", Sumgrade=" + Sumgrade + ", equalgrade=" + equalgrade + ", name=" + name + "]";
    }
}
=============================================================================
package c;

public class my1 {
	public static void main(String[] args) {
        Student c1=new Student();
        Student c2=new Student();
        c1.setnumber(2019110105);
        c1.setname("chen");
        c1.setEgrade(87);
        c1.setMgrade(78);
        c1.setCgrade(84);
        c1.Sum();
        c1.equal();
        System.out.println("总成绩为:"+c1.getSumgrade());
        System.out.println("平均成绩为:"+c1.getequalgrade());
        System.out.println(c1.toString());
        c2.Student(2019110104, "fang", 70, 78, 94);
        c2.Sum();
        c2.equal();
        System.out.println(c2.toString());
        c1.Compare(c2);
        StudentXW c3=new StudentXW();
        c3.Student(2019110106, "xi", 60, 78, 88);
        c3.getzeren("xuewei");
        c3.Sum();
        c3.testScore();
        System.out.println(c3.toString());
        StudentBZ c4=new StudentBZ();
        c4.Student(2019110109, "jin", 62, 78, 94);
        c4.getzeren("banzhang");
        c4.Sum();
        c4.equal();
        c4.testScore();
        System.out.println(c4.toString());
    }
}
class StudentXW extends Student{
	    public String zeren;
	    public void getzeren(String zeren) {
	        this.zeren=zeren;
	    }
	    public String setzeren() {
	        return zeren;
	    }
	    public void testScore() {
	        testscore=Sumgrade/3+5;
	    }

	    public String toString() {
	        return "StudentXW [name=" + name + ", number=" + number + ", Egrade=" + Egrade + ", Mgrade=" + Mgrade
	                + ", Cgrade=" + Cgrade + ", Sumgrade=" + Sumgrade + ", equalgrade=" + equalgrade + ", testscore="
	                + testscore + ", zeren=" + zeren + "]";
	    }
}
class StudentBZ extends Student{
    public String zeren;
    public void getzeren(String zeren) {
        this.zeren=zeren;
    }
    public String setzeren() {
        return zeren;
    }
    public void testScore() {
        testscore=Sumgrade/3+5;
    }

    public String toString() {
        return "StudentXW [name=" + name + ", number=" + number + ", Egrade=" + Egrade + ", Mgrade=" + Mgrade
                + ", Cgrade=" + Cgrade + ", Sumgrade=" + Sumgrade + ", equalgrade=" + equalgrade + ", testscore="
                + testscore + ", zeren=" + zeren + "]";
    }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...