这是代码.
public class Test {
class InnerClass{
}
public static void main(String[] args){
InnerClass ic = new InnerClass();
}
}
Run Code Online (Sandbox Code Playgroud)
它说错误信息
non-static variable this cannot be referenced from a static context
after creation of object ic.
Run Code Online (Sandbox Code Playgroud)
任何人都可以给我理由吗?
谢谢
在编写BookStoreApplication时,它使用Book,Tape和CD类来创建对象.虽然未完成,但应用程序类应创建新的BookStoreItems,即Book,Tape和CD.它们继承自BookStoreItems类.在这个应用程序类中,我不断收到错误:
error: non-static method printMenu() cannot be referenced from a static context
error: non-static method getUserChoice() cannot be referenced from a static context
error: non-static variable input cannot be referenced from a static context
Run Code Online (Sandbox Code Playgroud)
我把它改为静态然后不是静态的,但我继续得到这个错误......
import java.util.Scanner;
public class BookStoreApp2 {
//constants for options
static final int ADD_BOOK = 0;
static final int ADD_TAPE = 1;
static final int ADD_CD = 2;
static final int QUIT = -1;
Scanner input = new Scanner (System.in);
public static void main(String[] args) { …Run Code Online (Sandbox Code Playgroud) 对于我的CS课程,我必须编制一个酒店登记/结账系统.该计划必须能够检查人员进出.该计划在入住时为客人分配第一个可用房间.如果没有免费房间,它也会这么说.酒店有四个房间.
在作业中,它说需要有4个班级:Assignment5,Hotel,Room和Guest.
Assignment5类用于与用户交互.
酒店类有四个房间和所有操作房间的方法.
客房类有1位客人.如果客房是空的,客人可以办理入住手续.如果客人要离开,则需要清空房间.
访客类:访客具有名字和姓氏.
在菜单中需要有4个选项:1显示可用房间和职业的状态.2检查选项3检查选项4结束程序选项.
好的,所以我知道我应该为自己完成任务.但是,我不知道它是什么,但从作业开始,我有很大的问题切割小块的东西.另外这个任务是学习使用不同的类,我真的不明白在这种情况下我应该采取哪些步骤.
有人可以通过提供一些提示来帮助我开始吗?我一直盯着我的屏幕好几个小时,只是觉得我可以用一些小小的见解让我开始.任何帮助都非常赞赏!
好了,到目前为止帮忙了.
**首先,非常感谢你的帮助,你们真棒!已经连续7个小时了,仍然卡住了.我现在的问题是它不能编译.它说:
Java:28: checkIn(Gast) in Hotel cannot be applied to (java.lang.String)
hotel.checkIn("Guido");
^
1 error
Run Code Online (Sandbox Code Playgroud)
也许,有人看看我现在这样做的方式是在正确的道路上吗?我感谢JavaGeek的计划,但我想通过自己来学习它.
到目前为止,我有以下内容:
import java.util.Scanner;
public class bopgave5 {
public static void main(String[] args) {
boolean opnieuw = false;
do {
int invoer = menu();
if (invoer == 2){
Hotel hotel = new Hotel();
hotel.checkIn("Guido");
opnieuw = true;
}
else if (invoer == 4)
opnieuw = false;
else
opnieuw = true;
}
while (opnieuw == true); …Run Code Online (Sandbox Code Playgroud) 决议:Dev Env问题.重新启动Dev Env,一切正常.
第一次询问,所以如果我做错了,请告诉我.
我正试图在C#中使用特定于实例的变量.以下测试代码似乎应该可以工作,但会抛出错误:
非静态字段,方法或属性需要对象引用...
这样做的正确方法是什么,以便我有一个对于每个类的实例都是唯一的公共var,并且可以在函数中设置var(静态或其他)?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AlchemyWebSocketsTest2
{
class KHandler
{
public string name = "wut";
static void KHandlerInstantiate()
{
name = "huh";
Console.WriteLine("All Good.");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读Kathy和Bert SCJP 1.6并遇到以下代码:
class Bar {
int barNum = 28;
}
class Foo {
Bar myBar = new Bar();
void changeIt(Bar myBar) {
myBar.barNum = 99;
System.out.println("myBar.barNum in changeIt is " + myBar.barNum);
myBar = new Bar();
myBar.barNum = 420;
System.out.println("myBar.barNum in changeIt is now " + myBar.barNum);
}
public static void main (String [] args) {
Foo f = new Foo();
System.out.println("f.myBar.barNum is " + f.myBar.barNum);
f.changeIt(f.myBar);
System.out.println("f.myBar.barNum after changeIt is "
+ f.myBar.barNum);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然它是阴影变量的主题,但我无法理解如何在main()方法(静态)中引用非静态变量myBar?
我可以看到非静态类的静态对象不能在方法内创建?
码:
public class Rent {
public void abc() {
System.out.println("Non static method");
}
public static void def() {
System.out.println("this is static method");
}
}
public class SampleJava {
public static void main(String[] args) {
Rent r1 = new Rent();
public static Rent r2; //not allowed in static method
}
public static Rent r3; //allowed outside method
public void def() {
Rent r4 = new Rent();
public static Rent r5; //not allowed in non-static method either
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个超级课程:
open class A {
fun doStuff() {
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个子类扩展了:
class B: A() {
companion object {
doStuff() //compile error
}
}
Run Code Online (Sandbox Code Playgroud)
如何从伴侣对象调用我的doStuff()方法?
我正在尝试在 android 中创建一个 Logger 类,它仅在调试版本时才会记录消息。
object Logger2 {
private const val TAG = Constants.LOGGING_TAG
fun d(message : Any?){
if (BuildConfig.DEBUG)
Log.d(TAG , message.toString())
}
fun d(message: Any? , e : Exception?){
if (BuildConfig.DEBUG)
Log.d(TAG , message.toString(), e)
}
fun e(message : Any?){
if (BuildConfig.DEBUG)
Log.e(TAG , message.toString())
}
fun e(message: Any? , e : Exception?){
if (BuildConfig.DEBUG)
Log.e(TAG , message.toString(), e)
}
fun w(message : Any?){
if (BuildConfig.DEBUG)
Log.w(TAG , message.toString())
}
fun w(message: Any? , e : Exception?){
if …Run Code Online (Sandbox Code Playgroud) 我已经在互联网上搜索了这个错误的答案:
无法从静态上下文引用非静态方法'getStringExtra(java.lang.String)'
我没有找到任何东西,所以我来到这里.这是我用来添加信息作为额外内容的代码:
Intent OpenList = new Intent(this, ListRandom.class);
OpenList.putExtra("ListItem1",List.get(1));
OpenList.putExtra("ListItem2", List.get(2));
OpenList.putExtra("ListItem3", List.get(3));
OpenList.putExtra("ListItem4",List.get(4));
OpenList.putExtra("ListItem5", List.get(5));
Run Code Online (Sandbox Code Playgroud)
这里有额外的,我得到错误:
Intent OpenList = getIntent();
ListItem1 = Intent.getStringExtra("ListItem1");
ListItem2 = Intent.getStringExtra("ListItem2");
ListItem3 = Intent.getStringExtra("ListItem3");
ListItem4 = Intent.getStringExtra("ListItem4");
ListItem5 = Intent.getStringExtra("ListItem5");
Run Code Online (Sandbox Code Playgroud)
任何帮助都会受到赞赏,因为我正在成长为一名程序员!
我有一个类,在其中我有一些静态和一些非静态方法,所以当我试图从静态方法访问非静态方法时,我得到了那个着名的错误.每当我在这个论坛上搜索时,我会在有两个课程的时候得到解决方案.我的问题是如果它们在同一个类中,如何从静态方法调用非静态方法?
我正在努力
new ClassName().methodName();
Run Code Online (Sandbox Code Playgroud)
但我的方法包含发送Intent和finish(),所以如果我创建其他对象而不是完成不起作用.