search

Home  >  Q&A  >  body text

java - 程序正常运行,为什么还会提示name字段未使用?

class ClassExamole

    {
        static void Main(string[] args)
        {
            //Console.Read();
            Car car = new Car();
            Console.WriteLine(car.DoSmothing());//输出 BMW
            Console.Read();
        }
    }

class Car

{  
    private  string name;//波浪线,提示已赋值,但其值从未使用过
  
    public string DoSmothing()
    {
      
        return name = "BMW";
    } 
 }      
PHP中文网PHP中文网2811 days ago512

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 17:39:36

    The value of the field XXX is not used
    因为你是用private进行声明的name变量表明是私有的,如果你不进行setter和getter方法的设置,其他类是无法对其进行设置和取值的。只要设置了就不会有波浪线了,可以用alt+shift+s快捷键进行代码生成。
    

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:39:36

    The name field is indeed not used. . .

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:39:36

    You can avoid errors by initializing it in the constructor or code block

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:39:36

    The foundation is flawed, my child, so I have never used it. If you don't believe me, it's easy to just return "BMW". The declared name is private and needs to be retrieved and assigned using the get and set methods. What is returned now is not the name. The basics, you must grasp the basics well.

    reply
    0
  • Cancelreply