Giter Club home page Giter Club logo

Comments (2)

MarsonShine avatar MarsonShine commented on May 30, 2024

疑问的文章:https://github.com/MarsonShine/Books/blob/master/ThinkingInJava/docs/enumerated.md

问题:我认为文章中这部分意思传达错误:

//以下是尝试把枚举项传参
//是不允许这样的
class NotPromise {
    void method(LikesClass.ITEM1 instance){}
}

原文是“同样,在 Java 中枚举中的项是不能像 C# 中枚举一样可以在方法中当作参数传递的”

观点:在C#中也不能这样传递,我已经动手试过来了。 JAVA的枚举本质上是类,C#的枚举本质是一种特殊的简单struct。两者的区别是在设计和实现上的区别,在业务信息传达上面却是一样的

很抱歉才看到这个issue,的确是这样的。之前对java 枚举的理解有误。
如果在 java enum 申明一个变量,这个变量其实也是 static 的。
很显然下面的这种写法很让人困惑:

enum LikesClass {
    ITEM1 { void action(){ print("action1");}},
    ITEM2 { void action(){ 
        print("action2");
        state = 2;
    }},
    ITEM3 { void action(){ print("action3");}};
    abstract void action();
    int state = 0;
}

... main
LikesClass instance = LikesClass.ITEM2;
instance.action();
print(instance.state); // 这里的 state 变量到底是实例化变量还是静态变量?

就像上面的注释一下,实际上 java 是不允许这么写的:LikesClass.state。然后就想当然的做了文章后续的判断。

from books.

hjkl950217 avatar hjkl950217 commented on May 30, 2024

恩恩, JAJVA我了解的很少,C#里的枚举能肯定是静态的,JAVA我就不清楚了。 有可能他是一个枚举类,然后做了处理,让这个类new一个并静态化,静态的是类的引用,也能达到这样的效果

from books.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.