JavaPPT&练习题4

本文介绍了面向对象编程中类和对象的概念,包括访问修饰符(public, private, protected)、构造方法的使用以及如何初始化对象。通过Rectangle、Circle和Fan等示例,展示了如何定义和操作类的属性和方法。此外,还讨论了StopWatch和Stock类,用于计时和股票价格计算。
摘要由CSDN通过智能技术生成

对象与类


三种修饰符:public对所有其他类都可见
private对任何子类都不可见
protected子类可见


  • 绘制UML图时,public用+号,private用-号。
    9.1
class Rectangle{//我们创建对象
	private double width=1.0;
	private double height=1.0;
	public Rectangle(){}
	public Rectangle(double width,double height){
		this  .width = width;
		this  .height=height;
	}
	public double getArea(){
		return width*height;
	}
	public double getPerimeter(){
		return (width+height)*2;
	}
	public double getWidth(){
		return width;
	}
	public double getHeight(){
		return height;
	}
	public static void main (String[] args){
		Rectangle a = new Rectangle(4,40);
		Rectangle b = new Rectangle(3.5,35.9);
		System.out.println("a:"+a.getWidth()+" "+a.getHeight()+" "+a.getArea()+" "+a.getPerimeter());
		System.out.println("b:"+b.getWidth()+" "+b.getHeight()+" "+b.getArea()+" "+b.getPerimeter());
	}
}

UML图

Rectangle
-width:double -height:double
+getWidth +getHeight +getArea +getPerimeter

访问修饰符:getXXXX
修改器setXXX


对象初始化

class Circle{
	private double radius;
	public Circle(){
		redius =1;
	}
}

规则:类名与对象名保持一致。这叫构造方法。
构造方法是没有返回值类型的。
构造方法不能像方法一样被调用,它起作用的唯一时刻是用new创建对象时。


构造方法

class Circle{
	private double radius;
	public Circle{
		radius=1;
	}
	public double  getRadius(){
		return radius;
	}
	public static void main(String args[]){
		Circle c = new Circle();//这里c是我们创建的对象!
		System.out.println(c.getRadius());
	}
}

带参数的构造方法

class Circle{
	private double radius;
	public Circle(double r){
		radius=r;
	}
	public double  getRadius(){
		return radius;
	}
	public static void main(String args[]){
		Circle c = new Circle(2.55);//这里c是我们创建的对象!
		System.out.println(c.getRadius());
	}
}

9.8

class Fan{
	public static final  int SLOW=1;
	public static final  int MEDIUM=2;
	public static final  int FAST=3;

	private int speed ;
	private boolean on;
	private double radius;
	private String color;
	public Fan(){
		speed=SLOW;
		on = false;
		radius=5;
		color = new String( "green");
	}
	public int getSpeed(){
		return speed;
	}
	public void setSpeed(int s){
		speed=s;
	}
	public String toString(){
		return ("[s=" + speed + ",r=" + radius +",c="+color+" ]");
	}
	public static void main(String[] args){
		Fan f1 = new Fan();
		Fan f2 = new Fan();
		f1.setSpeed(MEDIUM);
		System.out.print(f1);
	}
	
}

9.6

class StopWatch{
	private long starTime;
	private long endTime;
	public static void main(String[] args){
		StopWatch time0 = new StopWatch();
		time0.stop();
		System.out.println(time0.getElapsedTime());
	}
	public StopWatch(){
		this.starTime=System.currentTimeMillis();
	}
	public void star(){
		this.starTime=System.currentTimeMillis();;
	}
	public void stop(){
		this.endTime=System.currentTimeMillis();;
	}
	public long getElapsedTime(){
		int sum=0;
		for(int i=0;i<100000000;i++)
			sum+=i;			
		return (this.endTime-this.starTime);
	}
	
	
}

9.2(Stock类)

class Stock{
	private String symbol;
	private String name;
	private double previousClosingPrice;
	private double currentPrice;
	public Stock(String symbol,String name,double currentPrice,double previousClosingPrice){
		this.symbol=symbol;
		this.name=name;
		this.currentPrice=currentPrice;
		this.previousClosingPrice=previousClosingPrice;
	} 
	public String getsymbol(){
		return symbol;
	}
	public String getname(){
		return name;
	}
	public double getChangePercent(){
		return ((currentPrice-previousClosingPrice)/previousClosingPrice)*100;
	}
	public static void main(String [] args){
		Stock s = new Stock("ORCL","Oracle Corporation",34.35,34.5);
		System.out.print("[symbol:"+s.getsymbol()+"  "+" name:"+s.getname()+"  "+"Percent:"+String.format("%.2f",s.getChangePercent())+"%]");
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值