當前位置:首頁 » 編程語言 » java的student類

java的student類

發布時間: 2022-05-04 19:08:23

A. 用java編寫student類

public class Person {
private String name;
private int age;

public void getInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}

==================================

public class Student extends Person {
private String school;

public void getInfo() {
super.getInfo();
System.out.println("School: " + school);
}

public static void main(String[] args) {
Student s = new Student();
s.setName("aaa");
s.setAge(28);
s.setSchool("bbb");
s.getInfo();
}

public String getSchool() {
return school;
}

public void setSchool(String school) {
this.school = school;
}

}
打字不易,如滿意,望採納。

B. 用java定義一個Student類,如何操作

package com.tgyt.nzft.core.listview;

public class Student {

private String s_no;//學號
private String s_name;//姓名
private boolean isLeader;//班幹部否
private String s_sex;//性別
private double s_shuxue;//數學
private double s_yuwen;//語文
private double s_waiyu;//外語

//學生信息錄入
public void addStudentInfo(String s_no, String s_name, boolean isLeader, String s_sex,
double s_shuxue, double s_yuwen, double s_waiyu) {
this.s_no = s_no;
this.s_name = s_name;
this.isLeader = isLeader;
this.s_sex = s_sex;
this.s_shuxue = s_shuxue;
this.s_yuwen = s_yuwen;
this.s_waiyu = s_waiyu;
}

//獲得總分
public double getTotal(){
return s_shuxue + s_yuwen + s_waiyu;
}

//平均分
public double getV(){
return getTotal() / 3;
}

public static void main(String[] args) {
Student student = new Student();
student.addStudentInfo("no_101", "女霸王", false, "女", 100, 35, 55);
System.out.println("總分:" + student.getTotal());
System.out.println("平局分:" + student.getTotal());
}
}

C. java定義一個學生類Student

1:
packagewo;
-
publicclassStudentText{
publicstaticvoidmain(String[]args){
Students1=newStudent(01,"張三");
Students2=newStudent(02,"李四");
booleanb=s1.equals(s2);
System.out.println(b);
}

}
classStudent{
privateintnum;
privateStringnema;

publicStudent(intnum,Stringnema){
super();
this.num=num;
this.nema=nema;
}
publicStudent(){

}
publicintgetNum(){
returnnum;
}
publicvoidsetNum(intnum){
this.num=num;
}
publicStringgetNema(){
returnnema;
}
publicvoidsetNema(Stringnema){
this.nema=nema;
}


publicbooleanequals(Objectobj){
if(this==obj)
returntrue;
if(obj==null)
returnfalse;
if(getClass()!=obj.getClass())
returnfalse;

if(num!=this.num)
returnfalse;
returntrue;
}

}
2

packagewo;

publicclasspolyTest{

publicstaticvoidmain(String[]args){
pensonp=newpenson("張三",25);
System.out.println(p);
loyeel=newloyee(01,3000);
System.out.println(l);
Managerm=newManager("老師");
System.out.println(m);

}

}
classpenson{
privateStringname;
privateintage;
publicpenson(){
super();
}
publicpenson(Stringname,intage){
super();
this.name=name;
this.age=age;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringtoString(){
return"penson[姓名:"+name+",年齡:"+age+"]";
}
}
classloyee{
privateintid;
privatedoublesalary;
publicloyee(){
super();
}
publicloyee(intid,doublesalary){
super();
}
publicintgetID(){
returnid;
}
publicvoidsetID(intiD){
id=iD;
}
publicdoublegetSalary(){
returnsalary;
}
publicvoidsetSalary(doublesalary){
this.salary=salary;
}
publicStringtoString(){
return"loyee[工號"+id+",工資:"+salary+"]";
}
}
classManager{
privateStringtype;
publicManager(){
super();
}
publicManager(Stringtype){
super();
this.type=type;
}
publicStringgetType(){
returntype;
}

publicvoidsetType(Stringtype){
this.type=type;
}
publicStringtoString(){
return"Manager[職務名稱:"+type+"]";}
}

D. java student類的創建

代碼為Test.java

classstudent{
privateStringsno;
privateStringsname;
privateStringssex;
privateintage;

publicstudent(Stringsno,Stringsname,Stringssex,intage){
super();
this.sno=sno;
this.sname=sname;
this.ssex=ssex;
this.age=age;
}
publicvoidgetInfo(){
System.out.println("學號:"+this.sno+".姓名:"+this.sname
+".性別:"+this.ssex+".年齡:"+this.age);
}
publicvoidsetAge(intage){
this.age=age;
}
}
publicclassTest{
publicstaticvoidmain(String[]args){
studentstu=newstudent("s31","張三","男",20);
//獲取學生信息
stu.getInfo();
//修改年齡
stu.setAge(30);
//獲取修改後的學生信息
System.out.println("修改後的學生信息為:");
stu.getInfo();
}
}

E. java.student類

class Student {
private String id;//表示學號
private String name;//表示姓名
private char sex;//表示性別
private int age;//表示年齡
private double computer_score;//表示計算機課程的成績
private double english_score;//表示英語課的成績
private double maths_score;//表示數學課的成績
private double total_score;// 表示總成績
private double ave_score; //表示平均成績

public Student(String id, String name){
this.id = id;
this.name = name;
}
public Student(String id, String name, char sex, int age){
this(id, name);
this.sex = sex;
this.age = age;
}
public String getId(){
return id;
}//獲得當前對象的學號,
public double getComputer_score(){
return computer_score;
}//獲得當前對象的計算機課程成績,
public double getMaths_score(){
return maths_score;
}//獲得當前對象的數學課程成績,
public double getEnglish_score(){
return english_score;
}//獲得當前對象的英語課程成績,

public void setId(String id){
this.id=id;
}// 設置當前對象的id值,
public void setComputer_score(double computer_score){
this.computer_score=computer_score;
}//設置當前對象的Computer_score值,
public void setEnglish_score(double english_score){
this.english_score=english_score;
}//設置當前對象的English_score值,
public void setMaths_score(double maths_score){
this.maths_score=maths_score;
}//設置當前對象的Maths_score值,

public double getTotalScore(){
return computer_score+maths_score+english_score;
}// 計算Computer_score, Maths_score 和English_score 三門課的總成績。
public double getAveScore(){
return getTotalScore()/3;
}// 計算Computer_score, Maths_score 和English_score 三門課的平均成績。

}

class Undergraate extends Student{
private String classID;

public Undergraate(String id, String name, char sex, int age,String classID){
super(id,name,sex,age);
this.classID=classID;
}
public String getClassID(){
return classID;
}
public void setClassID(String classID){
this.classID=classID;
}
}

class Graate extends Student{
private String tutor;

public Graate(String id, String name, char sex, int age,String tutor){
super(id,name,sex,age);
this.tutor=tutor;
}

public double getTotalScore() {
return getComputer_score()+getEnglish_score();
}
public double getAveScore(){
return getTotalScore()/2;
}
}
public class Test {
public static void main(String[] args) {
Undergraate ug=new Undergraate("0001", "張三", '男',19, "2083");
ug.setComputer_score(65);
ug.setEnglish_score(75);
ug.setMaths_score(85);
Graate g=new Graate("1001","李四",'女',24,"王五");
g.setComputer_score(65);
g.setEnglish_score(75);
g.setMaths_score(85);
Student s[]={ug,g};

for(int i=0;i<s.length;i++){
System.out.println("學號:"+s[i].getId()+" 計算機成績:"+s[i].getComputer_score()+
" 英語成績:"+s[i].getEnglish_score()+" 數學成績:"+s[i].getMaths_score()
+" 總成績:"+s[i].getTotalScore()+" 平均成績:"+s[i].getAveScore());
}
}
}

F. Java定義學生類student

importjava.util.*;
classStudent{
//這里為了方便賦值,減少代碼,我就用public了。
publicintid;
publicStringname;
publicStringbanji;
//重寫構造器,方便增加學生
publicStudent(intid,Stringname,Stringbanji){
this.id=id;
this.name=name;
this.banji=banji;
}
publicintgetId(){
returnid;
}
//輸出學生信息
publicStringtoString(){
System.out.println("學號:"+id);
System.out.println("姓名:"+name);
System.out.println("班級:"+banji);
}
}
publicclassMain{
publicstaticvoidmain(String[]args){
//增加單個學生
Students1=newStudent(1,"小童鞋_成er","1班");
//輸出學生信息
System.out.println(s1);

//批量增加學生,弄一個ArrayList
ArrayList<Student>list=newArrayList<Student>();
//比如插入10個
for(inti=1;i<=10;i++){
list.add(newStudent(i,"學生"+i,i+"班"));
}

//顯示學生
for(Iterator<Student>it=list.iterator();it.hasNext();){
System.out.println(it.next());
}

//刪除學生用ListIterator來弄
for(ListIterator<Student>lli=list.listIterator();lli.hasNext();){
//比如要刪除學號為1的學生
if(lli.next().getId()==1){
lli.remove();
}
}
//最後便利這個ListIterator就可以了
}
}

G. Java編程題: 編寫一個Student類,包含name和age屬性,提供有參構造方法

//*********************Student
import java.util.Objects;

public class Student {
public String name;
public int age;
Student(){}
Student(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){//重寫toString()
return "name: "+name+" age: "+age;
}
public boolean equals(Object o) {//重寫equals()
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
public int hashCode() {//重寫hashCode()
return Objects.hash(name)+age;
}
}

//********************StudentTest
import java.util.HashSet;

public class StudentTest {
public static void main(String[] args) {
HashSet<Student> hs=new HashSet<>();
Student s1=new Student("zs",19);//s1
Student s2=new Student("zs",19);//s2
Student s3=new Student("ls",19);//s3
hs.add(s1);//加入集合
hs.add(s2);//
hs.add(s3);//
//s1,s2,equals()返回true,s2不會加入集合
for(Student it:hs) {//遍歷HasSet
System.out.println(it);
}
}
}

H. 編寫一個簡單Java類Student類。 2. 編寫一個簡單Java類Student類

public class Student {
// 1)包含兩個屬性sno(學號)和age(年齡),屬性全部封裝。
private String sno;
private int age;

// ① 提供一個兩參構造方法對屬性賦值,另外提供一個無參構造方法。
public Student(String sno, int age) {
this.sno = sno;
this.age = age;
}

public Student() {
}

// ② 提供一個返回信息的方法,方法名getInfo( ),返回兩個屬性的信息。
public void getInfo() {
System.out.println("學號: " + sno + ", 年齡: " + age);
}

// ③ 提供getter和setter方法。
public String getSno() {
return sno;
}

public void setSno(String sno) {
this.sno = sno;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

I. java定義一個Student類,包含內容如下

publicclassStudent{
/**
*成員變數:學號、姓名、性別、班幹部否、數學、語文、外語
成員方法:輸入、總分、平均分
編程實現這個類,並調用相應的方法輸入數據,計算某個人總分和平均成績
附加要求:計算所有輸入學員的各科平均成績
*/
publicintidNum;
publicStringname;
publicStringsex;
publicStringclassLeader;
publicfloatmath;
publicfloatchinese;
publicfloatenglish;
publicStudent(intidNum,Stringname,Stringsex,StringclassLeader,
floatmath,floatchinese,floatenglish ){
//TODOAuto-generatedconstructorstub
this.idNum=idNum;
this.name=name;
this.sex=sex;
this.classLeader=classLeader;
this.math=math;
this.chinese=chinese;
this.english=english;
}
publicfloatTotalFraction(){
floattotal;
total=math+chinese+english;
returntotal;

}
publicfloatAverage(){
floataverage;
average=(math+chinese+english)/3;
returnaverage;
}


}



import java.util.*;


public class Main {


public static void main(String[] args) {

// TODO Auto-generated method stub


//成員變數:學號、姓名、性別、班幹部否、數學、語文、外語

Scanner scanner=new Scanner(System.in);

System.out.println("輸入學號");

int id=scanner.nextInt();

System.out.println("姓名");

String name=scanner.next();

System.out.println("性別");

String sex=scanner.next();

System.out.println("班幹部");

String classLeader=scanner.next();

System.out.println("數學");

float math=scanner.nextFloat();

System.out.println("語文");

float chinese=scanner.nextFloat();

System.out.println("英語");

float english=scanner.nextFloat();

Student stu=new Student(id, name, sex, classLeader, math, chinese, english);

System.out.println("總分"+stu.TotalFraction());

System.out.println("平均分"+stu.Average());

}


}



如果要保存多個人的話Student[] student保存對象

熱點內容
柱子箍筋加密區長度 發布:2025-05-14 10:18:29 瀏覽:351
雲伺服器和內網穿透哪個好 發布:2025-05-14 10:16:41 瀏覽:627
安徽新能源網路配置是什麼 發布:2025-05-14 10:06:24 瀏覽:630
pinode搭建伺服器 發布:2025-05-14 10:04:23 瀏覽:4
電腦伺服器ip名稱 發布:2025-05-14 10:01:09 瀏覽:749
connectorpython 發布:2025-05-14 09:48:50 瀏覽:763
配置不好怎麼辦 發布:2025-05-14 09:46:40 瀏覽:622
數據流程圖中的數據存儲是指 發布:2025-05-14 09:46:39 瀏覽:445
我的世界伺服器id前綴mod 發布:2025-05-14 09:45:53 瀏覽:831
完整後台網站源碼 發布:2025-05-14 09:45:46 瀏覽:455