首页 > 解决方案 > 从超类中的子类访问扫描仪对象?

问题描述

我是一个非常新的、相对缺乏经验的 Java 程序员。我正在做的项目只是对我当前技能的测试,我的目标是编写尽可能高效的程序。

本质上,我有三个类:A、B 和 C。B 扩展 A,C 扩展 B,但我希望 C 中的 Scanner 对象用于 A 中的 switch 语句(更大方法的一部分)。

我想要这个的原因是因为我不想重载A中的方法(复制粘贴具有不同参数的相同代码并不理想),并且我不想将所有类合并为一个(代码很简单足以做到这一点,但我想测试我对对象创建和使用的了解)。

这是一些代码:

import java.time.LocalDateTime;

public class WatchFace {

    // MASTER TIME
    
    LocalDateTime dateTimeObject = LocalDateTime.now();
    int hour = dateTimeObject.getHour();
    int minute = dateTimeObject.getMinute();
    
    // WATCH FACE METHOD
    
    public void watchFaceMethod() {

        // Code I'd like to utilize; this is my question for StackOverflow
        // switch (userInput) {
        //     case 1:
        //     // Intentionally do nothing
        //     break;
        //
        //     case 2:
        //     // Change minute and hour to some values obtained by timezone stuff
        //     break;
        //
        //     case 3:
        //     // Change both minute and hour to -1
        //     break;
        // }
        
        // Basically, the rest of this code just prints something different to the Windows CLI depending on the
        // hour and minute variables' current values (i.e. beyond the intended switch statement).
    }
}

import java.time.format.DateTimeFormatter;

public class Watch extends WatchFace {
    
    static void watchMethod() {
        
        // Code printing some Strings is here.
        
        WatchFace watchFaceObject = new WatchFace();
        watchFaceObject.watchFaceMethod();
        
        // Code printing some more Strings is here.
        
        DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("hh:mm a 'on' EEEE, MMMM dd, yyyy");
        String dateTimeDisplay = watchFaceObject.dateTimeObject.format(dateTimeFormat);
        System.out.print("\nIt is currently " + dateTimeDisplay + "\n");
        if (watchFaceObject.hour == 11 && watchFaceObject.minute == 11) {
            System.out.println("Make a wish!");
        }
    }
}
import java.util.Scanner;

public class InteractiveWatch extends Watch {
    public static void main(String[] args) {
        
        // WATCH OBJECT

        Watch watchObject = new Watch();
        
        // STARTUP

        System.out.println("Welcome to Interactive Watch!\n");
        System.out.println("What would you like to do?");
        System.out.println("[1] See local time.");
        System.out.println("[2] See local time in a particular place.");
        System.out.println("[3] See something special.\n");
        
        Scanner scannerObject = new Scanner(System.in);
        
        // INPUT
        
        boolean loopBreak = true;
        
        while (loopBreak) {
            
            loopBreak = false; // loopBreak set to false
            
            String userInput = scannerObject.nextLine(); // User inputs some string
            
            switch(userInput) {
                case "1":
                watchObject.watchMethod(); // watchFaceMethod ideally detects userInput == 1
                break;
                
                case "2":
                watchObject.watchMethod(); // watchFaceMethod ideally detects userInput == 2
                break;
                
                case "3":
                watchObject.watchMethod(); // watchFaceMethod ideally detects userInput == 3
                break;
                
                default:
                loopBreak = true; // loopBreak set to true; while loop reinitiates
                System.out.println("\nPlease enter a valid key.\n");
                break;
            }
        }
    }
}

我从 w3schools 的 Java 课程中学到了所有东西,但我还有很多东西要学。让我知道我想要的是否可能,或者其他任何可以使这段代码更有效率的东西。谢谢!

标签: javajava.util.scannersubclasssuperclass

解决方案


不,这是不可能的。

超类在 Java 中无权访问其子类的成员。但是子类可以访问其超类的所有非私有成员。


推荐阅读