在执行重构时,我最终创建了一个类似下面示例的方法.为简单起见,数据类型已更改.
我之前有一个这样的赋值语句:
MyObject myVar = new MyObject();
Run Code Online (Sandbox Code Playgroud)
它是偶然重构的:
private static new MyObject CreateSomething()
{
return new MyObject{"Something New"};
}
Run Code Online (Sandbox Code Playgroud)
这是我的剪切/粘贴错误的结果,但new关键字in private static new有效并编译.
问题:new关键字在方法签名中表示什么?我假设它是在C#3.0中引入的东西?
这有override什么不同?
我已经看到了这一点,但我想知道是否有可能强制覆盖未标记为虚拟或抽象类型的成员(因此不应该破坏基础对象的功能).
我特别指的是覆盖:
IDictionary<TKey, TValue> Dictionary { get; }
Run Code Online (Sandbox Code Playgroud)
在
KeyedCollection<TKey, TValue>
Run Code Online (Sandbox Code Playgroud)
使用另一个实现IDictionary的成员类型,以使用另一个内部存储集合.
是否有可能绕过限制?
请注意,我不想为此使用扩展(在我的情况下,它无论如何都不会有用).
UI InputField在获得焦点时会突出显示其中的所有文本。我想将插入符号移动到文本的末尾,以便用户可以继续在其中写入文本。目前,我有一个可以解决问题的hack解决方案,但是突出显示文本还有很短的时间。这是我的技巧:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class TextFieldBehaviour : MonoBehaviour, ISelectHandler
{
private InputField inputField;
private bool isCaretPositionReset = false;
void Start()
{
inputField = gameObject.GetComponent<InputField>();
}
public void OnSelect (BaseEventData eventData)
{
isCaretPositionReset = false;
}
void Update()
{
if(inputField.isFocused == true && isCaretPositionReset == false)
{
inputField.caretPosition = inputField.text.Length;
isCaretPositionReset = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我也在检查InputField 的源代码。但是我在创建没有SelectAll()函数的自定义代码时遇到了麻烦。由于的保护水平,我得到了很多错误UnityEngine.UI.SetPropertyUtility。
我试图更好地理解c#中的抽象类.
我知道在抽象类中你必须覆盖抽象方法并且可以覆盖虚方法.
我的问题是:
我可以覆盖非虚拟方法吗?(我知道通常我不能 - 但也许抽象类不同?)或者它会隐藏吗?
另外,正如我在这里读到的如何强制调用C#派生方法(第一个答案) - 在我看来,因为非虚拟方法在编译时静态链接而且无法更改 - 我无法调用方法在派生类中,从来没有?如果是这样 - 隐藏方法有什么意义?
为什么我无法访问S类方法?为什么我能够在M类中创建具有相同名称的方法?
public class S
{
public S()
{
}
public int myFunc(int a, int b)
{
return a + b;
}
}
public class M:S
{
public M()
{
}
public string myFunc(int a, int b)
{
return (a + b).ToString();
}
}
public class Test
{
public static void Main(string[] args)
{
M mm = new M();
mm.myFunc(1,2); // Why I am not able to access S class myFunc
}
}
Run Code Online (Sandbox Code Playgroud) 如果我有2个类,一个用于数据,例如:
public class Cords
{
public double x;
public double y;
}
Run Code Online (Sandbox Code Playgroud)
一,使用这些数据:
public class Geometry
{
public Cords()
{
points = new List<Cords>();
}
public void SomeMathWithPoints()
{
MagicWithPoints(points);
}
protected List<Cords> points;
}
Run Code Online (Sandbox Code Playgroud)
我想用一些特定的函数,使用继承来扩展这个类,但这次我需要一些Cords类的附加数据.所以我试着这样做:
public class ExtendedCords: Cords
{
public double x;
public double y;
public string name;
}
public class ExtendedGeometry : Geometry
{
protected SomeNewMagicWithPoints(){...}
protected List<ExtendedCords> points;
}
Run Code Online (Sandbox Code Playgroud)
但我注意到,如果我愿意:
ExtendedGeometry myObject = new ExtendedGeometry();
myObject.SomeMathWithPoints();
Run Code Online (Sandbox Code Playgroud)
此函数将使用旧(parrents)字段points.那么如何让它使用一个类型的新ExtendedCords?我的意思是,我希望能够在新领域使用child和parrent的功能.