我有游戏对象,我需要将它们放入列表中,如下所示:
List<GameObject> unityGameObjects = new List<GameObject>();
我想知道这两种实例化类/模型以及在 C# 中设置属性的方法中哪一种最快并且使用的资源更少。
假设类TestClass有一个名为 的字符串属性Info。
var instantiatedClass = new TestClass();
instantiatedClass.Info = "Information";
Run Code Online (Sandbox Code Playgroud)
或者
var instantiatedClass = new TestClass {
Info = "Information"
}
Run Code Online (Sandbox Code Playgroud)
当然,我想知道当您必须设置 10 多个属性时哪个最快。以上只是为了说明。
谢谢
(如果本文有更好的地方,请评论)
尝试编写一个返回 Pair 的迭代器:我的 PairIterator 的一部分
public Pair next() {
this.counter ++;
Pair p = new Pair(this.l.get(counter - 1), this.l.get(counter));
//error occurs here
}
public class Pair<E> {
private E e1;
private E e2;
public Pair(E e1, E e2) {
this.e1 = e1;
this.e2 = e2;
}
public E first() {
return this.e1;
}
public E second() {
return this.e2;
}
}
Run Code Online (Sandbox Code Playgroud)
得到
无法实例化类型 Pair
...虽然 Pair 不是抽象类/接口。为什么会发生这种情况?
我是 C# 初学者。
每次我在类中创建构造函数来实例化类时。
class OtherClass
{
void Main()
{
MyClass myClass = new MyClass();
}
}
class MyClass
{
public string text;
public int num;
public MyClass()
{
text = "something";
num = 12;
}
}
Run Code Online (Sandbox Code Playgroud)
但今天我看到了新的变体
class OtherClass
{
void Main()
{
MyClass myClass = new MyClass { num = 12, text = "something" };
}
}
class MyClass
{
public string text;
public int num;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下差异吗?
PS 抱歉我的英语。
这个问题是面向对象编程中的一个设计问题,我正在尝试寻找或思考最佳解决方案,并且不确定到目前为止我想到的解决方案。
问题:
我编写了一个名为 的类ComponentsHub,其想法是在其中存储运行程序所必需的所有主要对象。每个对象都是静态的、最终的和公共的。我使用公共访问修饰符来轻松通过静态导入访问对象或仅静态访问它。
package com.rtransfer.net.components;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import com.rtransfer.net.system.StorageManager;
public class ComponentsHub {
public static final Logger logger;
public static final Server server;
public static final StorageManager storageManager;
public static final SecurityManager securityManager;
public static final RequestForwarder requestForwarder;
public static final Authenticator authenticator;
public static final Uploader uploader;
public static final ConnectionHandler connectionHandler;
public static final Listener listener;
static {
logger = Logger.getLogger("rtransfer.net");
try {
FileHandler handler = new …Run Code Online (Sandbox Code Playgroud) 变量value的initialize方法LocationList填充在这些变化是由relected线014 print语句线015,但return在线路016认为哈希仍然是空的(向右滚动才能看到后返回值=>).
def random_point
x = rand * 2.0 - 1.0
y = rand * 2.0 - 1.0
until x**2 + y**2 < 1.0
x = rand * 2.0 - 1.0
y = rand * 2.0 - 1.0
end
return [x, y]
end
class LocationList < Hash
def initialize(node_list)
value = {}
node_list.each {|node| value[node] = random_point }
print value
return value
end
end
z = ["moo", "goo", "gai", …Run Code Online (Sandbox Code Playgroud) 我有一个这样的程序代码。
我的界面是
public interface MyInterface {
public void m1(String name);
public void m2(int num);
}
Run Code Online (Sandbox Code Playgroud)
我有一个实现上述接口的类。
public class World implements MyInterface {
public void m1(String name) {
System.out.println(name);
}
public void m2(int num){
System.out.println("Number is: "+num);
}
public static void main(String args[]) {
MyInterface ob1 = new World(); //How it is instantiated
MyInterface ob2 = new World(); //How this one too is instantiated
ob1.m1("Jaguar");
ob2.m2(5);
}
}
Run Code Online (Sandbox Code Playgroud) Dude car = new dude(name="honda", year=1998);
Dude park = new Dude(name="blah", year=1996);
car=park;
Run Code Online (Sandbox Code Playgroud)
我是OOP的新手,我有时会看到这些参考变量设置为彼此相等,即使它具有不同的值.也许我误会了什么.可以解释为什么你要设置一个彼此相等的对象?
创建线程时,我看到如下代码:
Runnable watdaheck = new Runnable()
{
System.out.println("java with time contradicts itself");
}
Run Code Online (Sandbox Code Playgroud)
据我所知,无法实例化接口,因此我无法理解我们如何编写Runnable()来创建匿名类。可以给接口一个参考,但是不能实例化多态性。
我想知道如何在 python 中初始化一个对象,只使用命名的 args,如果可能的话。

如果self.name = name 和self.age = age 的顺序互换,则错误在于初始化age。我将这些作为关键字参数提供给对象,那为什么还不够呢?我看到一个类深入 python 实例化,使用显式命名的关键字参数和它们的默认值(文件名=无),所以我认为 **kwargs 也可以工作。谢谢
instantiation ×10
c# ×4
java ×4
constructor ×2
class ×1
gameobject ×1
generics ×1
hash ×1
interface ×1
list ×1
object ×1
oop ×1
properties ×1
python ×1
return ×1
ruby ×1
runnable ×1