每日练习

/*
 * 利用面向对象的方法,设计类Circle计算园的面积
 * 
 */
public class TestCircle {
    public static void main(String[] args) {
        Circle c1 = new Circle();
        //c1.setR(2.0);
        
        System.out.println(c1.findAir());
    }
}

class Circle{
    double r;
    final double PI = 3.14;
    public Circle() {
        
    }
    //设置半径的值
    public double getR() {
        return r;
    }
    public void setR(double r) {
        this.r = r;
    }
    //计算园的半径
    public double findAir(){
        return r*r*PI;
        
    }
}
/*
 题目一:
 编写程序,定义三个重载方法并调用。方法名为mOL。
 三个方法分别接收一个int参数、两个int参数、一个字符串参数。分别执行平方运算并输出结果,
 相乘并输出结果,输出字符串信息。
 在主类的main ()方法中分别用参数区别调用三个方法。
题目二:
定义三个重载方法max(),第一个方法求两个int值中的最大值,
第二个方法求两个double值中的最大值,
第三个方法求三个double值中的最大值,并分别调用三个方法。
*/
public class TestOverLoad {
    public static void main(String[] args) {
        mOL(2);
        mOL(2, 3);
        mOL("123");
        System.out.println(max(2,6));
        System.out.println(max(3.6,7.8));
        System.out.println(max(2.5,8.6,4.8));
    }
    
    //第一题的重载
    public static void mOL(int a){
        System.out.println(Math.pow(a, 2));
    }
    public static void mOL(int a,int b){
        System.out.println(a*b);
    }
    public static void mOL(String str){
        System.out.println(str);
    }
    //第2题重载
    public static  int max(int a,int b){
        return a>b?a:b;
    }
    public static double max(double a, double b){
        return a>b?a:b;
    }
    public static double max(double a,double b,double c){
        double max =0;
        if(a>b && a>c){
            max =a;
        }else if(b>a && b>c){
            max =b;
        }else{
            max =c;
        }
        return max;
    }
}
*
 * 
 * 要求:(1)创建Person类的对象,设置该对象的name、age和sex属性,调用study方法,
 * 输出字符串“studying”,调用showAge()方法显示age值,调用addAge()方法给对象
 * 的age属性值增加2岁。
    (2)创建第二个对象,执行上述操作,体会同一个类的不同对象之间的关系。
 */
public class TestPerson {
    public static void main(String[] args) {
        Person p =new Person();
        p.study();
        p.showAge();
        p.addAge();
        
        Person p2 = new Person();
        p2.setAge(20);
        p2.showAge();
        p2.addAge();
        p2.study();
    }

}

class Person{
    private String name;
    private int age;
    private String sex;
    
    public Person() {
        
    }
    //study方法
    public void study(){
        System.out.println("studying");
    }
    //showAge 方法
    public void showAge(){
        System.out.println(age);
    }
    //调用addage方法
    public void addAge(){
        System.out.println(age+2);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}
原文地址:https://www.cnblogs.com/flei/p/6845571.html