gpt4 book ai didi

java - 新手尝试创建实例

转载 作者:行者123 更新时间:2023-12-01 19:11:55 24 4
gpt4 key购买 nike

过去一周我一直在学习java,我决定练习一下。我想创建一个实例来调用主类中的号码

我想要的类(class)名为“Clients”在主类中,我要求用户输入他的 ID int我想从主类调用ID到Clients类但是没有成功两者都在同一个 .java 文件中

你能帮我一下吗? 我在网上搜索,但我发现的代码太多了,我的大脑无法处理,我在这里乞求一个例子

int userID = userInput; //In main
int getUserID = new Client(); //My instance attemt. Failed

最佳答案

在第二行中,您尝试将新创建的类 Client 对象分配给基本类型变量 getUserID,但失败了。

int userID = userInput; //In main
int getUserID = new Client(); //My instance attemt. Failed

如果您想分配 Client 类的对象,您需要在 Client 类型或其父类中分配该对象。

例如,如果类 ClientParent 是 Client 的父类,即

Client extends ClientParent{
....
}

以下示例有效:

 ClientParent instance = new Client(); // This will work as ClientParent is the super class of Client.
Object instance1 = new Client(); // Object is a super class of any class.
Client instance2 = new Client();// You can assign in same type.

希望上面的例子能够澄清您的困惑。

关于java - 新手尝试创建实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59472818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com