当前位置:首页 » 编程语言 » 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-14 13:08:56 浏览:431
简单的vb编程 发布:2024-11-14 13:06:45 浏览:522
绿色linux 发布:2024-11-14 12:56:11 浏览:349
游戏本缓存 发布:2024-11-14 12:55:28 浏览:649
微软提供的编译软件 发布:2024-11-14 12:55:16 浏览:17
长沙java培训机构哪家好 发布:2024-11-14 12:40:53 浏览:229
外存储器硬盘能存储的高清电影数 发布:2024-11-14 12:33:23 浏览:265
python分号作用 发布:2024-11-14 12:31:50 浏览:223
方舟编译器下载要钱吗 发布:2024-11-14 12:29:20 浏览:62
jspoa源码 发布:2024-11-14 12:21:31 浏览:420