java - 程序正常运行,为什么还会提示name字段未使用?
PHP中文网
PHP中文网 2017-04-17 17:37:36
0
4
508

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中文网

认证高级PHP讲师

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

The name field is indeed not used. . .

左手右手慢动作

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

洪涛

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template