JAVA 面向对象的详解

36 阅读1分钟

package objClass;

public class obj {

public static void main(String[] args) {
    System.out.println("万事万物皆对象");
    Phone p = new Phone();
    p.playEmail();
    p.playGame();
    
    //匿名对象
    new Phone().playEmail();
    new Phone().playGame();
    new Phone().price=199;
    new Phone().showPrice(); // 0.0
    PhoneMall mall =new PhoneMall();
    mall.show(p);
    //匿名对象使用
    mall.show(new Phone());
    
    //方法重载 一个类中可以出现相同的方法名 根据传参类型判断调用哪个方法
    int[] arr=new int[]{1,2,3};
    char[] arr1= new char[]{'a','b','c'};
    System.out.println(arr);  //地址值
    System.out.println(arr1); // abc 
    
    //练习题 

// int a=10;

// int b=10;

// obj.method(a,b);

   //将对象作为参数传递给方法
    PassObject pass=new PassObject();
    pass.printAreas(new Circle(), 5);

}

public static void method(int a,int b) {
    if(a==10) {
        System.out.println("a="+100);
    }
    if(b==10) {
        System.out.println("b="+100);
    }
    System.exit(0);
}

}

class PhoneMall{ public void show(Phone phone) { phone.playEmail(); phone.playGame(); } }

class Phone{ double price;

public void playEmail() {
    
    System.out.println("发送邮件");
}

public void playGame() {
    System.out.println("玩游戏");
}

public void showPrice() {
    System.out.println("这手机是"+this.price);
}

}

//定义一个circle 类

class Circle{

double radius=0.5;

//圆的面积
public double findArea() {
    double area=radius*radius*Math.PI;
    return area;
}

}

class PassObject{ public void printAreas(Circle d,int time) {

    for (int i = 1; i <= time; i++) {
        d.radius=i;
      double num=    d.findArea();
      
      System.out.println("半径为"+i+"=======>面积为"+num);
    }
}

}