java面向对象学习

谁用面向对象对学生的学号,姓名,成绩进行管理

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值