其他分享
首页 > 其他分享> > 三道关于类与方法的例题

三道关于类与方法的例题

作者:互联网

三道类与方法的例题

public class Demo1 {
    private static int v = 100;
    public static int num;

    public static void guess() {
        if (num > v) {
            System.out.println("猜大了");
        } else if (num < v) {
            System.out.println("猜小了");
        }else{
                System.out.println("猜测成功");
            }
        }
        public static void main(String[] args){
            Demo1.num = 50;
            Demo1.guess();
    }
}
public class Circle {
    public int r;
    public final double PI=Math.PI;
    public void acreage(){
        System.out.println("圆的面积为:"+Math.PI*r*r);
    }
    public void perimeter(){
        System.out.println("圆的周长为:"+2*Math.PI*r);
    }

    public static void main(String[] args) {
        Circle circle=new Circle();
        circle.r=4;
        circle.acreage();
        circle.perimeter();
    }
}
public class Vehicle {
    public double speed;
    public double upSpeed;
    public double downSpeed;
    public String type;
    public void move(){
        System.out.println(""+type+"车"+"加速后的速度是"+""+upSpeed+","+"减速后的速度是"+downSpeed+"。");
    }
    public Vehicle(){
        System.out.println(speed);
        System.out.println(type);
    }
    public void speedUp(double s){
        upSpeed=s;
    }

    public void speedDown(double s) {
        downSpeed=s;
    }

    public static void main(String[] args) {
        Vehicle vehicle=new Vehicle();
        vehicle.speedUp(150.55);
        vehicle.speedDown(90.70);
        vehicle.type="福特";
        vehicle.move();
    }
}

 

 

 

 

 

 

标签:double,三道,System,public,关于,println,例题,void,out
来源: https://www.cnblogs.com/laoqi666/p/10908748.html