假设我有以下课程,我不允许改变:
public class C
{
public C() { CreateSideEffects(); }
public void M() { DoSomethingUseful(); }
}
Run Code Online (Sandbox Code Playgroud)
我必须在不调用构造函数的情况下调用M. 可能吗?
为什么我们将实例分配给上层类的实例?这样做的原因是什么?对于前者 为什么我们在下面使用此代码?
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)
我希望,我已经清楚了.提前致谢.
谁能解释这个错误:
实例头中的语法错误(预期的构造函数)
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)
谢谢
这是我的代码:
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) 我收到以下错误:
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)
任何想法为什么会这样?
有没有办法获得控件的实例谁是我的事件?
private void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox1.Text = "hi";
ThisControl.Text = "hi";
}
Run Code Online (Sandbox Code Playgroud)
排序这样这两条线会做同样的事情吗?就像"This"关键字一样,但对于事件控件而不是类.
这些天我的代码中发生了一些奇怪的事情.每次我必须构建一个新的成员函数,或者每次我回到之前定义的成员函数时,我认为:
"嘿,无论这个类的哪个实例调用它,这都是完全相同的过程,所以让它变得静止!"
而且我觉得它会减少内存消耗,因为每个实例都不必"携带"一个函数的实现,这是正确的吗?
所以不要像以下那样:
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
所以,我已经定义了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) 我最近学会了如何创建类,虽然我不完全确定我应该使用它们的位置和原因.我用它们来创建具有类似方法/属性的对象.
我试着制作一个插科打码,但我偶然发现了一个我无法找到答案的问题.
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语句都在类中?
非常感谢你
我尝试在类型实例的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应该是类型 …