谁用面向对象对学生的学号,姓名,成绩进行管理
package com.zhiyou100.test;
class Student{
String ID;
String name;
int age;
double score;
void setItem(String id,String n,int a,double s){
ID=id;
name=n;
age=a;
score=s;
}
void showInfo(){
System.out.println("学号:"+ID+"姓名:"+name+"年龄:"+age+"成绩:"+score);
}
}
public class StuTest {
public static void main(String[] args){
Student stu1=new Student();
Student stu2=new Student();
Student stu3=new Student();
stu1.setItem("00101", "小丽", 18, 85);
stu1.setItem("00102", "丽丽", 18, 85);
stu1.setItem("00105", "阿宝", 18, 85);
stu1.showInfo();
stu2.showInfo();
stu3.showInfo();
}
}
java对象的使用
package com.zhiyou100.test;
class Box {
double width;
double height;
double depth;
double volume(){
return width*height*depth;
}
void setDim(double w,double h,double d){
width=w;
height=h;
depth=d;
}
}
class BoxText{
public static void main(String[] s){
Box myBox=new Box();
Box hisBox=new Box();
double myVol,hisVol;
myBox.setDim(10,20,15);
hisBox.setDim(3,6,9);
myVol=myBox.volume();
hisVol=hisBox.volume();
System.out.println("myVol is "+myVol);
System.out.println("hisVol is "+hisVol);
}
}
对象的引用于对象的实体
package com.zhiyou100.test;
public class Animaltest {
public static void main(String[] args){
class Animal{
String color;
int age;
public void setColor(String c){
color=c;
}
public void setAge(int a){
age=a;
}
public void showInfo(){
System.out.println("动物的颜色是"+color);
System.out.println("动物的年龄是"+age);
}
}
Animal a=new Animal();
a.setColor("白色");
a.setAge(3);
a.showInfo();
}
}