想抽象个物体,但不懂如何开始,希望高手能举个例子详解..重要的是思想

解决方案 »

  1.   

    class car {
    String name = "奇瑞QQ";
    String color = "红";
    void run(){
    System.out.println("每小时80公里");
    }
    }这个意思?
      

  2.   

    不好意思,可能是我表达不清楚,刚开始接触java,老师叫我们每周抽象一个物体,什么物体都得,
    我抽象的是个苹果:import java.io.*;
                  public class apple{
                   String shape;
                    String colour;
    后面我就不知道怎么写下去了,还不知道对不对 所以想请高手帮忙指导下,最好举个例子并分析下,指导下思想,让我知道怎么抽象个物体.   
      

  3.   

    呵呵  是这样,没错。
    public class Apple {
    String shape; 
    String colour;
    Apple()
    {
    shape="";
    colour="";
    }
    public String eat()
    {
    //eating
    return "好吃!";
    }
    public String getShape()
    {
    return shape;
    }
    public String getColour()
    {
    return colour;
    }
    }
      

  4.   


    public class Apple {
       private String shape;
       private String color;
       // 无参数传递
       public Apple() {
          shape = "";
          color = "";
       }
       // 传递形状和颜色
       public Apple(String shape, String color) {
          this.shape = shape;
          this.color = color;
       }
       // 传递形状
       public Apple(String shape) {
          this.shape = shape;
          this.color = "";
       }
       // 传递颜色
       public Apple(String color) {
          this.color = color;
          this.shape = "";  
       }
       // 当然也可以通过SET方法传递
       public void setShape(String shape) { this.shape = shape; }
       public void setColor(String color) { this.color = color; }
    }应该是这样吧