class C {
static int b = 1;
void f1() {
System.out.println(C.b); // Access static variable indirectly
}
}
class A {
public static void main(String a[]) {
C as = new C();
as.f1();
}
}
class C {
int a = 0;
void f1() {
System.out.println(C
编辑:对不起。我好像有点糊涂了。我试图在静态方法中调用一个非静态方法,但它没有工作。他们是同一班的。这导致我(错误地)假设如果不实例化就不能调用非静态的,即使它们属于同一个类。既然我已经提出了这个问题,请允许我稍微修改一下这个问题。为什么不能在静态方法中调用非静态方法?这是我的代码,这样你就可以告诉我,如果我只是在胡说八道:
public class DataAccessObjectClass
{
public static IList<Records> GetAllRecords(Dictionary<string,string> searchDic)
好了,故事是这样的。
我有一个由[Assembly].LoadFrom()启动的程序集数组,这些程序集加载了许多动态链接库。被调用,它在此方法之外设置了许多变量,如下所示:
Public Class Example
Dim C As Integer = 0
Public Sub InitMain()
C = 50
End Sub
Public Sub Test()
MsgBox("C = " & C)
End Sub
End Class
如果我稍后在主应用程序中的某个地方使用相同的Assembl
有人知道为什么可以使用this()或super()在构造函数的第一行引用static方法,而不能引用非静态方法吗?
请考虑以下工作方式:
public class TestWorking{
private A a = null;
public TestWorking(A aParam){
this.a = aParam;
}
public TestWorking(B bParam)
{
this(TestWorking.getAFromB(bParam));
}
//It works because it
我定义Python单例如下:
class Database:
initialized = False
def __init__(self):
self.id = random.randint(1,101)
print("Generated an id of ", self.id)
print("Loading database from file")
_instance = None
def __new__(cls, *args, **kwargs):
if n
class A {
public $a = "BooMBa";
public function fun1() {
echo $this->a;
// echo (new self)->a;
}
}
class B extends A {
public static function fun2() {
return (new self)->fun1();
// return static::fun1();
}
}
B::fun2(); // returns B
我有下面的代码,我知道__new__是用来创建类的实例的,而__init__方法是用来初始化对象的值的。
是否只在返回基类的实例时调用__init__?
我想要的是返回一个名为class A的另一个类的实例。__init__有办法配置class A的实例吗?
# Python program to
# demonstrate __new__ method
# class whose object
# is returned
class GeeksforGeeks(object):
def __str__(self):
return "GeeksforGee
我有一个名为Location的类,其中有几个没有任何参数的方法。
但是,当我尝试用方法的结果创建一个变量时,它需要一个参数。为什么会这样呢?
Location类:
let locationManager = CLLocationManager()
public class Location {
public func coordinate() -> (latitude: Float?, longitude: Float?) {
let latitude = Float((locationManager.location?.coordinate.latitude
当我在Josh的Bloch书中回顾Builder模式时,我想出了一个更简单的实现,但我不确定它是否正确。例如:
public class Test {
public static void main(String[] args) {
Numbers first = new Numbers.Builder().setD(3.14).build();
System.out.println(first);
Numbers second = new Numbers.Builder().setI(17).setF(1.24F).build();
我开始学习Java,但我无法理解“Java中的思考”一书中的一个例子。在本例中,作者表示“简单使用' this‘关键字”:
//Leaf.java
//simple use of the "this" keyword
public class Leaf {
int i = 0;
Leaf increment() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
pub
我被困在调用另一个类的函数中的类。假设我有一个类A,函数是self.B和c,
class A
def self.b
//does something
end
def c
//does something
end
end
和另一个类D
class D
before_create :x
def x
//have to call the class A and its functions here
end
end
请告诉我怎么弄这个。
据我所知,你可以这样实例化这个类:
$class = new className();
然后,要在其中使用方法,您只需执行以下操作:
$class->myMethod();
或者,如果你想在不实例化的情况下从类中使用某些东西,你可以这样做:
className::myMethod();
我确信我以前使用过后者,没有任何问题,但是为什么我得到一个错误,说:
Fatal error: Using $this when not in object context
我用于调用它的代码是:
// Display lists with error message
manageLists::displ