java學生類
❶ java創建一個學生類
publicclassTest{
publicstaticvoidmain(String[]args){
ArrayList<Student>studentList=newArrayList<>();
studentList.add(newStudent("張三","class1","Java",85));
studentList.add(newStudent("周樂兒","class1","C#",79));
studentList.add(newStudent("王濤","class2","C#",52));
studentList.add(newStudent("李明","class2","Java",48));
//===========================================================================
System.out.println("姓名 班級名 課程名 考試成績");
for(Studentstudent:studentList){
System.out.println(student);
}
//===========================================================================
floatclass1SumScore=0;
intclass1StudentCount=0;
for(Studentstudent:studentList){
if(student.isClass1()){
class1SumScore+=student.getScore();
class1StudentCount++;
}
}
System.out.println(" class1班的總分:"+class1SumScore+" 平均分:"+class1SumScore/class1StudentCount);
//===========================================================================
floatjavaScore=0;
floatjavaStudentCount=0;
floatcSharpScore=0;
floatcSharpStudentCount=0;
for(Studentstudent:studentList){
if(student.isJava()){
javaScore+=student.getScore();
javaStudentCount++;
}elseif(student.isCSharp()){
cSharpScore+=student.getScore();
cSharpStudentCount++;
}
}
System.out.println(" Java課程的平均分:"+javaScore/javaStudentCount+" C#課程的平均分:"+cSharpScore/cSharpStudentCount);
}
}
classStudent{
privateStringname;
privateStringclassName;
privateStringcourseName;
privateintscore;
publicStudent(Stringname,StringclassName,StringcourseName,intscore){
this.name=name;
this.className=className;
this.courseName=courseName;
this.score=score;
}
publicbooleanisClass1(){
return"class1".equals(className);
}
publicbooleanisJava(){
return"Java".equals(courseName);
}
publicbooleanisCSharp(){
return"C#".equals(courseName);
}
publicStringgetName(){
returnname;
}
publicStringgetClassName(){
returnclassName;
}
publicStringgetCourseName(){
returncourseName;
}
publicintgetScore(){
returnscore;
}
@Override
publicStringtoString(){
returnname+" "+className+" "+courseName+" "+score;
}
}
❷ java編寫程序創建學生類
年齡、性別等 方法:愛好方法等等 2.創建2個學生子類
❸ JAVA創建一個學生類
public class Student {
private String id;
private String name;
private int age;
private String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
public class StudentToDo {
/**
* 吃飯方法
*/
public void eat(Student student){
System.out.println("id"+student.getId()+"年齡為"+student.getAge()+"性別是"+student.getSex()+"姓名叫"+student.getName()+"的學生在吃飯");
}
/**
* 打豆豆方法
*/
public void play(Student student){
System.out.println("id"+student.getId()+"年齡為"+student.getAge()+"性別是"+student.getSex()+"姓名叫"+student.getName()+"的學生在打豆豆");
}
}
public class Test {
public static void main(String[] args) {
StudentToDo studentToDo = new StudentToDo();
Student student = new Student();
student.setId("1");
student.setAge(15);
student.setName("張三");
student.setSex("男");
studentToDo.eat(student);
studentToDo.play(student);
}
}
❹ JAVA程序編寫一個學生類Student
Public Class Student{
Public long id;
Public String name;
Public int age;
Public boolean sex;
Public long phone;
Public Student(long i,String n,int a,boolean s,long p){
this.id = i;
this.name = n;
this.age = a;
this.sex = s;
this.phone = p;
}
Public int getage(){
return this.age;
}
Public long getphone(){
return this.phone;
}
Public String toString(){
return this.name;
}
Public static void main(String[]args){
Student student = new Student(01,"李明",20,true,13xxx);
System.out.println(student.age());
System.out.println(student.getphone());
System.out.println(student.toString());
}
}
❺ 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+"]";}
}
❻ JAVA創建一個具體的學生類
//學生類
public class Student{
private String stu_id;
private String stu_name;
private float math;
private float chinese;
private float computer;
public void setStu_id(String stu_id){
this.stu_id=stu_id;
}
public String getStu_id(){
return stu_id;
}
public void setStu_name(String stu_name){
this.stu_name=stu_name;
}
public String getStu_name(){
return stu_name;
}
public void setMath(float math){
this.math=math;
}
public float getMath(){
return math;
}
public void setChinese(float chinese){
this.chinese=chinese;
}
public float getChinese(){
return chinese;
}
public void setComputer(float computer){
this.computer=computer;
}
public float getComputer(){
return computer;
}
}
//主方法的類
public class Start{
public static void main(String[] args){
Student stu1=new Student();
stu1.setStu_id("No1");
stu1.setStu_name("張三");
stu1.setMath(89.5f);
stu1.setChinese(70f);
stu1.setComputer(98f);
Student stu2=new Student();
stu2.setStu_id("No2");
stu2.setStu_name("李四");
stu2.setMath(79.5f);
stu2.setChinese(90f);
stu2.setComputer(68f);
float stu1Sum = stu1.getMath()+stu1.getChinese()+stu1.getComputer();
float stu2Sum = stu2.getMath()+stu2.getChinese()+stu2.getComputer();
if(stu1Sum > stu2Sum){
System.out.println ("總分最高分為:"+stu1Sum);
System.out.println ("學號:"+stu1.getStu_id());
System.out.println ("姓名:"+stu1.getStu_name());
System.out.println ("數學:"+stu1.getMath());
System.out.println ("語文:"+stu1.getChinese());
System.out.println ("計算機:"+stu1.getComputer());
}else if(stu1Sum < stu2Sum){
System.out.println ("總分最高分為:"+stu2Sum);
System.out.println ("學號:"+stu2.getStu_id());
System.out.println ("姓名:"+stu2.getStu_name());
System.out.println ("數學:"+stu2.getMath());
System.out.println ("語文:"+stu2.getChinese());
System.out.println ("計算機:"+stu2.getComputer());
}else{
System.out.println ("總分一樣高為:"+stu1Sum);
System.out.println ("學號:"+stu1.getStu_id());
System.out.println ("姓名:"+stu1.getStu_name());
System.out.println ("數學:"+stu1.getMath());
System.out.println ("語文:"+stu1.getChinese());
System.out.println ("計算機:"+stu1.getComputer());
System.out.println ("學號:"+stu2.getStu_id());
System.out.println ("姓名:"+stu2.getStu_name());
System.out.println ("數學:"+stu2.getMath());
System.out.println ("語文:"+stu2.getChinese());
System.out.println ("計算機:"+stu2.getComputer());
}
}
}
希望對你有幫助!
❼ java作業求設計一個學生類
不難,就是費時間
❽ 在java中編寫程序,定義一個學生類
代碼如下:
package exam2;
import java.util.ArrayList;
import java.util.List;
/**
編寫一個Java應用程序,該程序包括3個類:
定義一個學生類,Student有姓名,學號,選學的課程列表
定義一個課程類:課程名稱,課程分數
(1)初始化一個學生,選擇了三門課程,並添加到學生的屬性中
(2)實現統計學生的總分功能
1.正確編寫2個類(5分)
2.統計學生的總分功能(5分)
*/
public class Test {
public static void main(String[] args) {
List<Course> courses = new ArrayList<>();
// 初始化3門課程及分數
Course course1 = new Course("java", 80);
Course course2 = new Course("Math", 60);
Course course3 = new Course("English", 90);
// 課程對象添加到集合
courses.add(course1);
courses.add(course2);
courses.add(course3);
// 初始化學生對象
Student student = new Student("tom", "2015101", courses);
// student.setStuid("2015101");
// student.setStuname("tom");
// student.setCourses(courses);
// System.out.println(student);
// (2)實現統計 學生 的總分功能
int sum = sumScore(courses);
System.out.println(student.getStuname() + "學生總分:" + sum);
}
private static int sumScore(List<Course> courses) {
int sum = 0;
// 用課程對象,來獲取課程的分數
for (Course course : courses) {
sum += course.getScore();
}
return sum;
}
}
package exam2;
import java.util.List;
public class Student {
// 定義一個學生類,Student有姓名,學號,選學的課程列表
private String stuname;
private String stuid;
private List<Course> courses;
public Student() {
}
public Student(String stuname, String stuid, List<Course> courses) {
this.stuname = stuname;
this.stuid = stuid;
this.courses = courses;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
@Override
public String toString() {
return "學生姓名=" + stuname + ",學號=" + stuid + ", 課程=" + courses;
}
}
package exam2;
public class Course {
//定義一個課程類:課程名稱,課程分數
private String cname;
private int score;
public Course() {
}
public Course(String cname, int score) {
this.cname = cname;
this.score = score;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "[課程名稱=" + cname + ", 課程分數=" + score + "]";
}
}
/*
運行:
tom學生總分:230
*/
(8)java學生類擴展閱讀:
Public語句說明
1.Public語句聲明的變數在所有應用程序的所有沒有使用OptionPrivate Mole的模塊的任何過程中都是可用的;若該模塊使用了OptionPrivate Mole,則該變數只是在其所屬工程中是公用的。
2.使用Public語句可以聲明變數的數據類型。例如,下面的語句聲明了一個Integer類型的變數。
3.Public NumberOfEmployees As Integer 也可以使用Public語句來聲明變數的對象類型。下面的語句為工作表的新實例聲明了一個變數。
參考資料:網路-Public 語句
❾ JAVA 學生類
class
Students
{
private
String
xuehao;
private
String
name;
private
String
riqi;
public
void
setxuehao(String
xuehao)
{
this.xuehao=xuehao;
}
public
void
setname(String
name)
{
this.name=name;
}
public
void
setriqi(DEtime
riqi)
{
this.riqi=riqi.getTime();
}
public
String
getname()
{
return
name;
}
public
String
getriqi()
{
return
riqi;
}
public
String
getxuehao()
{
return
xuehao;
}
}
class
DEtime
{
int
year;
int
month;
int
day;
public
String
getTime()
{
return
year+"-"+month+"-"+day;
}
}
class
Test
{
Students
student=new
Students();
student.setxuehao("001");
student.setname("shine");
DEtime
time=new
DEtime();
time.year="2009";
time.month="01";
time.day="01";
Student.setriqi(time);
System.out.println("student學生的學號是"+student.getxuehao());
System.out.println("student學生的姓名是"+student.getname());
System.out.println("student學生的出生日期是"+student.getriqi());
}
因為
我忘記了
時間的表達方式
暫且用
String
來代替下把
不過
在日常生活中
我們都是用String
來做
更為簡單!