标签: instance

在不调用构造函数的情况下调用实例方法

假设我有以下课程,我不允许改变:

public class C
{
    public C() { CreateSideEffects(); }
    public void M() { DoSomethingUseful(); }
}
Run Code Online (Sandbox Code Playgroud)

我必须在不调用构造函数的情况下调用M. 可能吗?

c# methods constructor instance

1
推荐指数
2
解决办法
856
查看次数

将实例分配给上层类的实例

为什么我们将实例分配给上层类的实例?这样做的原因是什么?对于前者 为什么我们在下面使用此代码?

List lst = new LinkedList();
Run Code Online (Sandbox Code Playgroud)

似乎List是LinkedList的上层.为什么我们需要使用上层'实例而不是继承类',LinkedList的实例.

另外,我还有一个问题.

我已经看到一些代码将类实例分配给它的接口.为什么我们需要下面的例子呢?我知道,因为我们无法生成接口的实例,所以它允许我们使用接口的实例.但是,使用接口实例有什么意义呢?

Apple a = new Apple();
IFruit b = (IFruit) a; (IFruit is the interface of Apple)
Run Code Online (Sandbox Code Playgroud)

我希望,我已经清楚了.提前致谢.

java inheritance interface instance

1
推荐指数
1
解决办法
463
查看次数

为什么在Haskell中的实例定义中期望构造函数

谁能解释这个错误:

实例头中的语法错误(预期的构造函数)

class Nullable v where
  default_val :: v


instance Num a => Nullable a where  -- error reported here
  default_val = 0::a
Run Code Online (Sandbox Code Playgroud)

谢谢

haskell class instance

1
推荐指数
1
解决办法
407
查看次数

我无法访问光标的位置(以编程方式移动鼠标)

这是我的代码:

    private void MoveCursor(int x, int y)
    {
        // Set the Current cursor, move the cursor's Position,
        // and set its clipping rectangle to the form. 

        System.Windows.Forms.Cursor cursorMouse = new System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle);
        cursorMouse.Position = new System.Drawing.Point(x, y);
        System.Windows.Forms.Cursor.Clip = new System.Drawing.Rectangle(cursorMouse.Position, cursorMouse.Size);
    }
Run Code Online (Sandbox Code Playgroud)

这就是我的控制台所说的:

 Error  11  Member 'System.Windows.Forms.Cursor.Position.get' cannot be accessed with an instance reference; qualify it with a type name instead    F:\Win8\Kinect\InterfaceController\celmaibun\KinectToolbox\KinectToolbox\GesturesViewer\MainWindow.xaml.cs  1314    13  NkGesturesViewer
 Error  12  Member 'System.Windows.Forms.Cursor.Position.get' cannot be accessed with an instance reference; qualify it with a …
Run Code Online (Sandbox Code Playgroud)

c# mouse position instance cursor

1
推荐指数
1
解决办法
2849
查看次数

数组是必需的,但是找到了java.lang.String Java类数组错误

我收到以下错误:

array required, but java.lang.String found
Run Code Online (Sandbox Code Playgroud)

我不知道为什么。

我想做的是将一个对象的实例(我认为这是正确的术语)放入该类型的类(对象的)数组中。

我上课:

public class Player{
     public Player(int i){
           //somecodehere
     }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的main方法中创建它的一个实例:

static final Player[] a = new Player[5]; // this is where I'm trying to create the array.
public static void main(String[] args){
     Player p = new Player(1);
     a[0] = p; //this is the line that throws the error
}
Run Code Online (Sandbox Code Playgroud)

任何想法为什么会这样?

java arrays object instance

1
推荐指数
1
解决办法
2079
查看次数

获取控件的实例我是谁

有没有办法获得控件的实例谁是我的事件?

    private void TextBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox1.Text = "hi";
        ThisControl.Text = "hi";
    }
Run Code Online (Sandbox Code Playgroud)

排序这样这两条线会做同样的事情吗?就像"This"关键字一样,但对于事件控件而不是类.

c# instance visual-studio

1
推荐指数
1
解决办法
86
查看次数

什么是非静态函数c ++?

这些天我的代码中发生了一些奇怪的事情.每次我必须构建一个新的成员函数,或者每次我回到之前定义的成员函数时,我认为:

"嘿,无论这个类的哪个实例调用它,这都是完全相同的过程,所以让它变得静止!"

而且我觉得它会减少内存消耗,因为每个实例都不必"携带"一个函数的实现,这是正确的吗?

所以不要像以下那样:

class Foo
{
    int _attributes;

    double methods(int parameters)
    {
        // do stuff using..
    _attributes;

    };
};
Run Code Online (Sandbox Code Playgroud)

我最终得到的结果如下:

class Foo
{
    int _attributes;

    static double methods(int parameters, Foo* instance)
    {
        // do stuff using..
    instance->_attributes;

    };
};
Run Code Online (Sandbox Code Playgroud)

而且我看不到任何不会以这种方式改变的功能.我的所有功能都转向静态,我觉得某处出了点问题.

我错过了什么?再次使用非静态方法有什么用?XD

c++ static function member instance

1
推荐指数
1
解决办法
102
查看次数

lambda抽象ADT的显示实例

所以,我已经定义了lambda数据类型:

data LExpr
    = Variable String         -- variable
    | Apply LExpr LExpr       -- function application
    | Lambda String LExpr     -- Lambda abstraction 
    deriving (Eq, Show)  
Run Code Online (Sandbox Code Playgroud)

现在我想实现Show自己的一个实例.我已经完成了show'大部分工作的功能,但没有使用实例:

 show' :: LExpr -> String
 show' (Variable a) = a
 show' (Apply e1 e2) = "(" ++ show' e1 ++ " " ++ show' e2 ++ ")"                   
 show' (Lambda x e) = "(? " ++ x ++ ". " ++ show' e ++ ")"    
Run Code Online (Sandbox Code Playgroud)

我如何实现它,以获得以下输出而不使用显式show'函数:

Main> (Apply (Lambda …
Run Code Online (Sandbox Code Playgroud)

haskell instance show typeclass

1
推荐指数
1
解决办法
191
查看次数

如何在ruby中调用实例变量

我最近学会了如何创建类,虽然我不完全确定我应该使用它们的位置和原因.我用它们来创建具有类似方法/属性的对象.

我试着制作一个插科打码,但我偶然发现了一个我无法找到答案的问题.

class Person
  def initialize(name,health)
    @name = name
    @hp = health
  end

  def kick
    @hp -= 1
    if (@hp <= 0)
      puts "#{@name} got REKT!"
    end
  end

end

#Friends
michael = Person.new("Michael", 10)

10.times { michael.kick }
Run Code Online (Sandbox Code Playgroud)

即使这段代码有效,我想知道是否可以在课堂外使用/调用mihchael的hp?也许有点像哈希?michael[@hp]?但即使我将hp设置为全局变量,这也不起作用.是否所有检查对象属性的if/else语句都在类中?

非常感谢你

ruby variables if-statement instance

1
推荐指数
1
解决办法
935
查看次数

如何在Haskell实例中使用类型变量

我尝试在类型实例的where-body中使用type变量.但是GHC不对类型实例中的函数使用类型变量.

我尝试实现类型类Bits[a].

instance forall a. Bits a => Bits [a] where
    xor = zipWith xor
    rotateL list dis = keeped .|. overlap
    where
        overlap = tail moved ++ [head moved] 
        (keeped, moved) = unzip $ map (\n -> let rot = rotate n dis in (rot.&.mask, rot.&.filter)) list
        mask = (complement 0) `shiftL` dis -- this line
        filter = complement mask
Run Code Online (Sandbox Code Playgroud)

GHC说:

Could not deduce (Num a) arising from the literal ‘0’
Run Code Online (Sandbox Code Playgroud)

预期:

0应该是类型 …

haskell types instance

1
推荐指数
1
解决办法
88
查看次数