简体   繁体   English

Java静态或继承变量

[英]Java static or inheritance variable

Let's say I have 3 classes A, B, C, and my Main. 假设我有3个班级A,B,C和我的主要班级。 B extends A. I want to use a scanner in all of them include my main. B扩展A.我想在所有这些中使用扫描仪包括我的主要扫描仪。 should I move scanner by inheritance or should I use static and declare my scanner in my main? 我应该通过继承移动扫描仪还是应该使用静态并在我的主要部分声明我的扫描仪?

I tried to look here but did not get a clear answer which is better: Is there any way I can use a Scanner object in a different class? 我试着看看这里,但没有得到一个更好的明确答案: 有没有办法可以在不同的类中使用Scanner对象?

public class Main {
    public static Scanner staticScanner = new Scanner (System.in);
public static void main(String[] args) {
    Scanner sc = new Scanner (System.in);
    A a = new A(sc);
    C c = new C();
    c.cDoSomething(sc);
    sc.close();
    }

public class A {
    private Scanner sc;
    public A (Scanner sc){
    this.sc = sc;
    }
    public void aDoSomething(){
         B b = new B();
         b.bDoSomething(sc);

    }

public class B extends A {
    public void bDoSomething(Scanner sc){

    }

public class C {
    public void cDoSomething(Scanner sc){

    }

Like I said above I would like to understand which method is better and is the correct one to use. 就像我上面说的那样,我想了解哪种方法更好,并且是正确的方法。 staticScanner and call it by my main class or move my Scanner sc between classes as needed staticScanner并由我的主类调用它或根据需要在类之间移动我的Scanner sc

I would recommend you avoid a global static variable that is accessible from everywhere. 我建议你避免从任何地方访问的全局静态变量。

I would wrap a Scanner into a custom class, let's call it Reader , and pass the latter in as a dependency for classes that require reading. 我将Scanner包装到一个自定义类中,让我们称之为Reader ,并将后者作为依赖项传递给需要读取的类。 The class Scanner is a very simple and too concrete thing to bound your class to. Scanner类是一个非常简单和过于具体的东西,可以绑定你的类。

Neither Scanner nor Reader I would pass as a method argument. 我不会将ScannerReader作为方法参数传递。 The parent A can open read-only access to the field for its subclasses by a protected getter, for example. 例如,父A可以通过受保护的getter打开对其子类的字段的只读访问权限。

I think the answer here can depend on some of the specifics of the underlying classes and situation, particularly in regards to how coupled the state of the scanner might be to the state of other variables in the classes and how many separate methods or invocations are required. 我认为这里的答案可能取决于底层类和情境的一些细节,特别是关于扫描程序的状态如何与类中其他变量的状态耦合以及需要多少单独的方法或调用。

Generally, however, I personally prefer c.doSomething(sc); 但是,一般来说,我个人更喜欢c.doSomething(sc); if there is no internal state being kept in class C between method invocations. 如果在方法调用之间没有在C类中保留内部状态。 Ie we do not have c.doSomething1(sc) and c.doSomething2(sc) that need to be called in order against the same scanner due to internal state being modified in C . 即我们没有c.doSomething1(sc)c.doSomething2(sc) ,由于内部状态在C被修改,需要针对同一扫描器进行调用。

The counter argument being if we are making what is effectively a wrapper (or some form of decorator) around the Scanner that it makes perfect sense to pass the scanner in to the class in the constructor and keep a reference to it. 反问题是如果我们正在制作一个有效的扫描器周围的包装器(或某种形式的装饰器),那么将扫描器传递给构造函数中的类并保持对它的引用是完全合理的。 Ie if we want to have something like: 即如果我们想要有类似的东西:

MyReader reader = new MyReader(scanner);
MyObject myObject = reader.readNextMyObject();

However, I think it would be very confusing if we were reading from the same scanner via multiple wrappers interleaved in code: 但是,如果我们通过代码中交错的多个包装器从同一个扫描器读取,我认为会非常困惑:

// I think the below would be UGLY, at least imho.
MyReaderA readerA = new MyReaderA(scanner);
MyReaderB readerB = new MyReaderB(scanner);
MyObjectX obj1 = readerA.readMyObject();
MyObjectY obj2 = readerB.readMyObject();

I think that would be confusing that two different readers are operating on the same scanner and moving its index. 我认为两个不同的读者在同一个扫描仪上运行并移动其索引会让人感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM