Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Apr 2016
    Bài viết
    55

    Giúp mình lỗi này trong java với

    Mình mới học java bạn nào giúp mình với. Mình làm một bài test này khi chạy thì nó báo lỗi "non-static variable this cannot be referenced from a static context" mà ko tim thấy lỗi ở đâu.
    import java.lang.*;
    import java.io.*;
    class test1
    {
    class Moto
    {
    String make;
    String color;
    boolean engin;
    void startEngine(){
    if (engin)
    System.out.println("the engine is already");
    else {
    engin= true;
    System.out.println("the engine is now on");
    }
    }
    void showAtts() {
    System.out.println("this motocycle is a" + color + "" + make + ".");
    if (engin)
    System.out.println("the engine is on.");
    else System.out.println("the engine is off.");
    }
    }
    public static void main(String args[]){
    Moto m = new Moto();
    m.make = "Yamaha";
    m.color = "yellow";
    System.out.println("calling showatt...");
    m.showAtts();
    System.out.println("starting engine...");
    m.startEngine();
    System.out.println("calling showatt...");
    m.showAtts();
    System.out.println("starting engine...");
    m.startEngine();}
    };

  2. #2
    Ngày tham gia
    Nov 2015
    Bài viết
    2
    Bạn thử so sánh thử xem đoạn code của mình với của bạn khác nhau chỗ nào nha(đừng nới là kiểu chữ với màu chữ nha/[IMG]data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAA l21bKAAAAA1BMVEXh5PJm+yKVAAAAAXRSTlMAQObYZgAAAApJR EFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=[/IMG]):
    package test;
    import java.lang.*;
    import java.io.*;
    class test1
    {
    static class Moto
    {
    String make;
    String color;
    boolean engin;
    void startEngine(){
    if (engin)
    System.out.println("the engine is already");
    else {
    engin= true;
    System.out.println("the engine is now on");
    }
    }
    void showAtts() {
    System.out.println("this motocycle is a" + color + "" + make + ".");
    if (engin)
    System.out.println("the engine is on.");
    else System.out.println("the engine is off.");
    }
    }
    public static void main(String args[]){
    Moto m = new Moto();
    m.make = "Yamaha";
    m.color = "yellow";
    System.out.println("calling showatt...");
    m.showAtts();
    System.out.println("starting engine...");
    m.startEngine();
    System.out.println("calling showatt...");
    m.showAtts();
    System.out.println("starting engine...");
    m.startEngine();}
    };

  3. #3
    Ngày tham gia
    Aug 2015
    Bài viết
    2
    static class Moto

    Nên xem lại cách dùng static

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •