當前位置:首頁 » 編程語言 » java創建類

java創建類

發布時間: 2022-01-16 07:27:26

java學生類的創建和使用

package student;

public class Student {

private static int count;

/**
* 班級號
*/

private String classNo;

/**
* 年齡
*/

private int age;

/**
* 性別
*/

private char sex;

/**
* 姓名
*/

private String name;

/**
* 學號
*/

private String stuNo;

public Student(String stuNo, String classNo, String name, char sex, int age) {
super();
this.age = age;
this.classNo = classNo;
this.name = name;
this.sex = sex;
this.stuNo = stuNo;
count++;
}

public static int getExsitingStudentCount() {
return count;
}

public String getClassNo() {
return classNo;
}

public void setClassNo(String classNo) {
this.classNo = classNo;
}

public int getAge() {
return age;
}

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

/**
*
* @return F為男,M為女
*/
public String getSexString() {
return sex == 'F' ? "男" : "女";
}

public char getSex() {
return sex;
}

public void setSex(char sex) {
this.sex = sex;
}

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;
}

private String prop(String name, Object value, char sep) {
return sep + name + ":" + value;
}

public String toString() {
String desc = prop("學號", stuNo, '\0');
desc += prop("班號", classNo, ',');
desc += prop("姓名", name, ',');
desc += prop("性別", getSexString(), ',');
desc += prop("年齡", age, ',');
return desc;
}
}

import student.Student;

public class StudentTest {
public static void main(String[] args) {
Student s1 = new Student("No.1", "T1", "張三", 'F', 14);
Student s2 = new Student("No.1", "T1", "馬莉", 'M', 13);
println(s1);
println(s2);
s1.setName("李四");
s1.setAge(12);

println("姓名", s1.getName());
println("年齡", s1.getAge());
printlnSeparator();
if (s1.getAge() > s2.getAge()) {
println(s1);
} else
println(s2);
}
private static void printlnSeparator(){
System.out.println("=============================");
}
public static void println(Student s) {
println("學號", s.getStuNo());
println("班級", s.getClassNo());
println("姓名", s.getName());
println("性別", s.getSexString());
println("年齡", s.getAge());
printlnSeparator();
}

public static void println(String prop, Object value) {
System.out.println(prop + ":" + value);
}
}

Ⅱ java建立一個類

幫你寫個看看:


/**
*人類
*有『年齡』,『性別』,『身高』,『體重』,『職業』屬性
*@author
*
*/
publicclassPerson{
protectedintage;//年齡
protectedStringsex;//性別
protectedStringhigh;//身高
protectedStringweight;//體重
protectedStringskill;//職業

publicPerson(){
}

publicintgetAge(){
returnage;
}

publicvoidsetAge(intage){
this.age=age;
}

publicStringgetSex(){
returnsex;
}

publicvoidsetSex(Stringsex){
this.sex=sex;
}

publicStringgetHigh(){
returnhigh;
}

publicvoidsetHigh(Stringhigh){
this.high=high;
}

publicStringgetWeight(){
returnweight;
}

publicvoidsetWeight(Stringweight){
this.weight=weight;
}

publicStringgetSkill(){
returnskill;
}

publicvoidsetSkill(Stringskill){
this.skill=skill;
}

publicStringtoString(){

return"年齡:"+age+""+"性別:"+sex+""+"身高:"+high+""+"體重:"+weight+""+"職業:"+skill;
}
}

/**
*教師類有『學歷』屬性,『收入』屬性
*
*@authorAdministrator
*
*/
{
privateStringecation;//學歷
privateStringincome;//收入

publicTeacher(){
}

publicStringgetEcation(){
returnecation;
}

publicvoidsetEcation(Stringecation){
this.ecation=ecation;
}

publicStringgetIncome(){
returnincome;
}

publicvoidsetIncome(Stringincome){
this.income=income;
}

publicStringtoString(){

returnsuper.toString()+""+"學歷:"+ecation+""+"收入:"
+income;
}
}

/**
*學生類有『學歷』『是否在談戀愛』屬性
*
*@authorAdministrator
*
*/
{
privateStringecation;//學歷
privatebooleanisInLove;//是否在談戀愛

publicStudent(){
}

publicStringgetEcation(){
returnecation;
}

publicvoidsetEcation(Stringecation){
this.ecation=ecation;
}

publicbooleanisInLove(){
returnisInLove;
}

publicvoidsetInLove(booleanisInLove){
this.isInLove=isInLove;
}

publicStringtoString(){

returnsuper.toString()+""+"學歷:"+ecation+""+"是否在談戀愛:"
+isInLove;
}
}

/**
*官員類
*有收入屬性
*@authorAdministrator
*
*/
{
privateStringincome;//收入

publicOfficial(){
}

publicStringgetIncome(){
returnincome;
}

publicvoidsetIncome(Stringincome){
this.income=income;
}

publicStringtoString(){

returnsuper.toString()+""+"收入:"+income;
}
}


/**
*測試類
*@authorAdministrator
*
*/
publicclassTest{
publicstaticvoidmain(String[]args){
Personperson=newPerson();
person.setSex("男");
person.setAge(20);
person.setHigh("1.70m");
person.setWeight("128kg");
person.setSkill("神經病");
System.out.println("人類-"+person);//列印人

Teacherteacher=newTeacher();
teacher.setSex("男");
teacher.setAge(30);
teacher.setHigh("1.80m");
teacher.setWeight("128kg");
teacher.setSkill("教師");
teacher.setIncome("月薪10000");
teacher.setEcation("本科");
System.out.println("教師-"+teacher);//列印老師

Studentstudent=newStudent();
student.setSex("女");
student.setAge(10);
student.setHigh("1.60m");
student.setWeight("98kg");
student.setSkill("學生");
student.setEcation("小學");
student.setInLove(true);
System.out.println("學生-"+student);//列印學生


Officialofficial=newOfficial();
official.setSex("人妖");
official.setAge(50);
official.setHigh("1.40m");
official.setWeight("158kg");
official.setSkill("村長");
official.setIncome("月薪100000");
System.out.println("官員-"+official);//列印官員

}
}
//把每個類放到單獨的java文件裡面去
//運行Test類
//希望對你有幫助

Ⅲ 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();
}
}

Ⅳ 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,創建類

publicclassClock{
privateinthour;
privateintminute;
privateintsecond;

publicClock(){
this.hour=0;
this.minute=0;
this.second=0;
}

publicClock(inthour,intminute,intsecond){
this.hour=hour;
this.minute=minute;
this.second=second;
}

publicintgetHour(){
returnhour;
}

publicvoidsetHour(inthour){
this.hour=hour;
}

publicintgetMinute(){
returnminute;
}

publicvoidsetMinute(intminute){
this.minute=minute;
}

publicintgetSecond(){
returnsecond;
}

publicvoidsetSecond(intsecond){
this.second=second;
}

publicvoidchangeTime(inth,intm,ints){
if(h<0||h>23){
System.out.println("hour值必須在0-23之間!");
return;
}
if(m<0||m>59){
System.out.println("minute值必須在0-59之間!");
return;
}
if(s<0||s>59){
System.out.println("second值必須在0-59之間!");
return;
}
setHour(h);
setMinute(m);
setSecond(s);
}

publicvoidstep(){
if(second==59){
second=0;
minute++;
}
if(minute>59){
minute=0;
hour++;
}
if(hour>23){
hour=0;
}
}

publicvoidprint(){
StringhourStr=hour<10?"0"+hour:hour+"";
StringminuteStr=minute<10?"0"+minute:minute+"";
StringsecondStr=second<10?"0"+second:second+"";
System.out.println(hourStr+":"+minuteStr+":"+secondStr);
}


publicstaticvoidmain(String[]args){
Clockc=newClock();
c.print();
c.changeTime(23,10,10);
c.print();
c.changeTime(25,0,0);
c.print();
}
}

Ⅵ java類創建方法

這哪是什麼類創建方法啊.就是一個方法.這個方法的作用是創建一個OrderModel 並賦值該類相應的欄位.

public OrderModel getOrderModel()
//方法名為:getOrderModel,返回類型OrderModel
{

OrderModel orderModel = new OrderModel();
//新建一個OrderModel

orderModel.setOrderId(orderId);
orderModel.setDate(date);
orderModel.setShipped(shipped);
orderModel.setPurchases(purchases); //設置相應欄位

return orderModel; //返回新建的orderModel
}

Ⅶ 創建java類

public class TestFinal{
final Person p;//一個Person類型的空引用類變數

public TestFinal(){
this.p =new Person();//在構造器中初始化
}

public TestFinal(Person p){//從外界得到Person引用
this.p = p;
}
.
.
.

}

Ⅷ java怎樣建類

Java新建一個類,可以通過如下形式:

publicclassA{//public關鍵字可以去掉,class是一個關鍵字,是必須寫上的。A是類名,由創建者決定,但要符合規則:只要以下劃線或者字母開頭,後面字母數字或者下劃線。
inti;//可以定義多個變數
publicvoidfun(){//可以定義多個方法,個數為0或多個
}
}

Ⅸ JAVA創建一個學生類

//創建一個學生類,該類中擁有name,age等成員變數,
public class Student {

private String name;

private int age;

public String getName() {
return this.name;
}

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

// 要求 1)創建學生類,實現構造方法重載
public Student() {

}

public Student(String name) {
this.setName(name);
}

public Student(int age) {
this.setAge(age);
}

// 2)在某個構造方法中要求使用this()的方式調用其它構造方法
public Student(String name, int age) {
this(name);
this.setAge(age);
}

// 3)創建一個成員方法,該方法能返回姓名的長度
public int getNameLength() {
if (this.name != null) {
return this.name.length();
} else {
return 0;
}
}

// 4)創建一個成員方法,該方法能返回年齡
public int getAge() {
return this.age;
}

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

// 5)創建一個成員方法,該方法能返回姓名的首字元
public char getFirstCharacter() {
if ((this.name != null) && (this.name.length() > 0)) {
return this.name.toCharArray()[0];
} else {
return 0;
}
}
}

Ⅹ 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());
}
}
}

希望對你有幫助!

熱點內容
資料庫設計模板 發布:2024-11-15 00:47:25 瀏覽:825
編程的悟性 發布:2024-11-15 00:47:24 瀏覽:733
主流可編譯語言 發布:2024-11-15 00:42:23 瀏覽:729
excel緩存清除 發布:2024-11-15 00:39:53 瀏覽:486
機械鍵盤可編程 發布:2024-11-15 00:39:09 瀏覽:912
php判斷字元開頭 發布:2024-11-15 00:35:33 瀏覽:507
網易蘋果游戲怎麼轉移到安卓 發布:2024-11-15 00:07:52 瀏覽:270
win7php環境搭建 發布:2024-11-15 00:06:55 瀏覽:17
erpjava 發布:2024-11-14 23:52:23 瀏覽:253
電腦版地平線四怎麼連上伺服器 發布:2024-11-14 23:46:42 瀏覽:472