对象与类
三种修饰符: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())+"%]");
}
}