码迷,mamicode.com
首页 > 编程语言 > 详细

java类与对象,用程序解释

时间:2017-03-11 18:34:53      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:cti   int   card   new   static   []   name   第一个   属性   

//第一个
1
public class Cardemo{ 2 public static void main(String[] args){ 3 Car carwind = new Car(60,"red","west"); 4 carwind.speed = 60; 5 carwind.color = "red"; 6 carwind.direction = "west"; 7 8 System.out.println("The speed of car is " + carwind.speed); 9 System.out.println("The color of car is " + carwind.color); 10 System.out.println("The direction of car is " + carwind.direction); 11 } 12 } 13 class Car{ 14 //定义四个成员变量(属性) 15 int speed; 16 String color; 17 String name; 18 String direction; 19 }

第二个

public class Cardemo{
    public static void main(String[] args){
        Car carwind = new Car(60,"red","west"); //创建一个Car类的引用
        carwind.speed = 60;
        carwind.color = "red";
        carwind.direction = "west";
        
        System.out.println("The speed of car is " + carwind.speed);
        System.out.println("The color of car is " + carwind.color);
        System.out.println("The direction of car is " + carwind.direction);
        carwind.drivecar();
        carwind.speedup(5);
        System.out.println("The speed of car is " + carwind.speed);
        
    }
}
class Car{
    //定义四个成员变量(属性)
    int speed;
    String color;
    String name;
    String direction;
    
    Car(int speed, String color, String direction){
        this.speed = speed;
        this.color = color;
        this.direction = direction;
    }
    
    public void drivecar(){
        System.out.println("here we go!");
    }
    public void speedup(int aspeed){
        this.speed = speed + aspeed;
    }
}

 

java类与对象,用程序解释

标签:cti   int   card   new   static   []   name   第一个   属性   

原文地址:http://www.cnblogs.com/xiaolei-meow/p/6535668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!