JAVA 面向对象的详解
发布日期:2021-06-30 15:37:17 浏览次数:3 分类:技术文章

本文共 1421 字,大约阅读时间需要 4 分钟。

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

转载地址:https://jsonll.blog.csdn.net/article/details/107601427 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java 面向对象 构造器的详解
下一篇:java数组详解

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月22日 12时11分54秒