目前我有很多类(5)只有2个属性,但有不同的名称用于不同的目的:
public class Class1
{
public Class1()
{
}
public string Id { get; set; }
public string Value { get; set; }
}
public class Class2
{
public Class2()
{
}
public string Id { get; set; }
public string Value { get; set; }
}
........
public class Class5
{
public Class5()
{
}
public string Id { get; set; }
public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我为每个类都有一个返回a的方法List<Class>.
public static List<Class1> GetClass1()
{
Dictionary<string, …Run Code Online (Sandbox Code Playgroud) 我Output()在两个类Hour和方法中都有相同的代码Day.有没有办法避免它在一个地方而不是两个地方改变代码?
class Program
{
static void Main(string[] args)
{
Hour hour = new Hour("20150715 080000");
Day day = new Day(hour);
Console.WriteLine(String.Format("Hour: {0}", hour.Output()));
Console.WriteLine(String.Format("Day: {0}", day.Output()));
}
}
public interface IMoment
{
string OutputMoment();
}
class Hour : IMoment
{
public string Date;
public string Time;
public Hour (string s)
{
string[] parts = s.Split(';');
this.Date = parts[0];
this.Time = parts[1];
}
public string Output()
{
return Date + " " + Time;
} …Run Code Online (Sandbox Code Playgroud) 我有这个问题很长一段时间.我试图想象3门问题,只是为了乐趣和练习Swift.所以我有:
3扇门,因此所有门都有3种不同的IBAction和3种功能.这些功能都完全相同,但每个代码中只有门的数量不同.所以我想知道,我可以缩短这段代码吗?:
func openSecondChoice(whatDoorIsClickedOn: Int)
{
if whatDoorIsClickedOn == 1
{
if whatDoorIsClickedOn == doorWithNumber
{
UIButtonDoor1.setBackgroundImage( UIImage (named: "doorWithMoney"), for: UIControlState.normal)
}
else
{
UIButtonDoor1.setBackgroundImage( UIImage (named: "doorWithGoat"), for: UIControlState.normal)
}
}
if whatDoorIsClickedOn == 2
{
if whatDoorIsClickedOn == doorWithNumber
{
UIButtonDoor2.setBackgroundImage( UIImage (named: "doorWithMoney"), for: UIControlState.normal)
}
else
{
UIButtonDoor2.setBackgroundImage( UIImage (named: "doorWithGoat"), for: UIControlState.normal)
}
}
if whatDoorIsClickedOn == 3
{
if whatDoorIsClickedOn == doorWithNumber
{
UIButtonDoor3.setBackgroundImage( UIImage (named: "doorWithMoney"), for: UIControlState.normal)
}
else
{
UIButtonDoor3.setBackgroundImage( …Run Code Online (Sandbox Code Playgroud) condition ?
domElement.classList.add('show') :
domElement.classList.remove('show');
Run Code Online (Sandbox Code Playgroud)
上面的代码可以工作,但DOM变量和classList被显式输入两次.有没有办法使用三元组只将链中的差异部分放在各自的真/假条款中?
我想的是:
domElement.classList condition ? .add('show') : .remove('show');
Run Code Online (Sandbox Code Playgroud)
任何和所有输入都非常感谢.
javascript code-duplication ternary-operator method-chaining
我已经使用clap.
在其中,我有几个结构体和枚举,其中包含特定子命令的这些结构体。
我现在遇到了某种类型的代码重复问题,想知道 Rust 语言是否有一些我不知道的功能可以消除重复。
这是一个 MRE:
pub trait Runner {
fn run(&self);
}
pub struct Foo;
impl Runner for Foo {
fn run(&self) {
println!("Fooing");
}
}
pub struct Bar;
impl Runner for Bar {
fn run(&self) {
println!("Baring");
}
}
pub enum Action {
DoFoo(Foo),
DoBar(Bar),
}
impl Runner for Action {
fn run(&self) {
match self {
Self::DoFoo(runner) => runner.run(),
Self::DoBar(runner) => runner.run(),
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,模式匹配<Action as Runner>::run()在所有变体上都具有几乎相同的代码,因为所有枚举变体的元组都包含一个实现Runner.
在这个 …
我需要将平均分数与字母等级相匹配。那意味着
if (90 < avg && avg < 100) {
return 'A';
}
Run Code Online (Sandbox Code Playgroud)
依此类推,直到带有5个if-else语句的'F'。
这是很多重复,并且我要匹配的范围具有相同的长度。
有没有更有效的方法可以做到这一点?我不想重复if-else语句5次。