在C#中的自定义数据类型转换为字符串

huangapple go评论101阅读模式
英文:

Custom data type to string conversion in C#

问题

我试图解决这个问题,其中我需要将其他变量放入string acc中,但我不知道如何做到这一点。尝试创建自定义数据类型并将其转换为字符串,但这不起作用。

有人可以帮助我吗?

这是问题的链接: https://i.stack.imgur.com/wReLU.jpg

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        class Person
        {
            private string name;
            private string dob;

            public Person()
            {
            }

            public Person(string name, string dob)
            {
                this.name = name;
                this.dob = dob;
            }

            public void setName(string name)
            {
                this.name = name;
            }

            public void setDob(string dob)
            {
                this.dob = dob;
            }

            public string getName()
            {
                return name;
            }

            public string getDob()
            {
                return dob;
            }
        }

        struct Account
        {
            public int account_id;
            public double amount;
            public string accType;
        }

        class Employee : Person
        {
            private string name;
            private string dob;
            public Account account;
            private string acc;

            public Employee()
            {
            }

            public Employee(string name, string dob, string acc)
            {
                this.name = name;
                this.dob = dob;
                this.acc = acc;
            }

            public void setAcc(string acc)
            {
                this.acc = acc;
            }

            public void display()
            {
                Console.WriteLine("Name: " + getName());
                Console.WriteLine("Date of birth: " + getDob());
                Console.WriteLine("Account Id: " + account.account_id);
                Console.println("Amount: " + account.amount);
                Console.WriteLine("Account Type: " + account.accType);
            }
        }

        class Customer : Person
        {
            private string name;
            private string dob;
            public Account account;
            private string acc;

            public Customer()
            {
            }

            public Customer(string name, dob, string acc)
            {
                this.name = name;
                this.dob = dob;
                this.acc = acc;
            }

            public void setAcc(string acc)
            {
                this.acc = acc;
            }

            public void display()
            {
                Console.WriteLine("Name: " + getName());
                Console.WriteLine("Date of birth: " + getDob());
                Console.WriteLine("Account Id: " + account.account_id);
                Console.WriteLine("Amount: " + account.amount);
                Console.WriteLine("Account Type: " + account.accType);
            }
        }

        static void Main(string[] args)
        {
            Account account;

            account.account_id = 7478126;
            account.amount = 12000;
            account.accType = "Savings";
            string act = Convert.ToString(account);

            Employee em = new Employee();
            Console.WriteLine("Employee details:");
            em.setName("L");
            em.setDob("11.11.1999");
            em.setAcc(act);
            em.display();
        }
    }
}
英文:

I'm trying to solve this problem where I'll have to put other variables inside string acc, but I don 't know how to do that. Tried with creating a custom data type and converting it to string but that's not working.

Can anyone help me with this ?

This is the question: https://i.stack.imgur.com/wReLU.jpg

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
class Person
{
private string name;
private string dob;
public Person()
{
}
public Person(string name, string dob)
{
this.name = name;
this.dob = dob;
}
public void setName(string name)
{
this.name = name;
}
public void setDob(string dob)
{
this.dob = dob;
}
public string getName()
{
return name;
}
public string getDob()
{
return dob;
}
}
struct Account
{
public int account_id;
public double amount;
public string accType;
}
class Employee : Person
{
private string name;
private string dob;
public Account account;
private string acc;
public Employee()
{
}
public Employee(string name, string dob, string acc)
{
this.name = name;
this.dob = dob;
this.acc = acc;
}
public void setAcc(string acc)
{
this.acc = acc;
}
public void display()
{
Console.WriteLine("Name: " + getName());
Console.WriteLine("Date of birth: " + getDob());
Console.WriteLine("Account Id: " + account.account_id);
Console.WriteLine("Amount: " + account.amount);
Console.WriteLine("Account Type: " + account.accType);
}
}
class Customer : Person
{
private string name;
private string dob;
public Account account;
private string acc;
public Customer()
{
}
public Customer(string name, string dob, string acc)
{
this.name = name;
this.dob = dob;
this.acc = acc;
}
public void setAcc(string acc)
{
this.acc = acc;
}
public void display()
{
Console.WriteLine("Name: " + getName());
Console.WriteLine("Date of birth: " + getDob());
Console.WriteLine("Account Id: " + account.account_id);
Console.WriteLine("Amount: " + account.amount);
Console.WriteLine("Account Type: " + account.accType);
}
}
static void Main(string[] args)
{
Account account;
account.account_id = 7478126;
account.amount = 12000;
account.accType = "Savings";
string act = Convert.ToString(account);
Employee em = new Employee();
Console.WriteLine("Employee details:");
em.setName("L");
em.setDob("11.11.1999");
em.setAcc(act);
em.display();
}
}
}

答案1

得分: 2

以下是翻译好的内容:

"你现在有的要求真是糟糕。它们让你使用Java风格的方法,尽管你在写C#。它们规定了一个继承结构(图表),但代码本身并没有使用这个结构。而且它们让你使用不合适的类型。

你引入了一个 struct Account,尽管要求中并没有提到使用这样的结构。

以下是可能的正确写法:

abstract class Person
{
    public string Name { get; private set; }
    public DateOnly Dob { get; private set; }
    public string Account { get; private set; }

    public Person(string name, DateOnly dob, string account)
    {
        this.Name = name;
        this.Dob = dob;
        this Account = account;
    }

    public override string ToString() =>
        String.Join(Environment.NewLine,
            new[]
            {
                $"Name: {this.Name}",
                $"Date of birth: {this.Dob.ToShortDateString()}",
                $"Account Type: {this.Account}",
            });
}

class Employee : Person
{
    public Employee(string name, DateOnly dob, string account)
        : base(name, dob, account) { }
}

class Customer : Person
{
    public Customer(string name, DateOnly dob, string account)
        : base(name, dob, account) { }
}

static void Main(string[] args)
{
    Employee em = new Employee("L", new DateOnly(1999, 11, 11), "Savings");
    Console.WriteLine("Employee details:");
    Console.WriteLine(em.ToString());
}

希望这对你有所帮助。"

英文:

The requirements you have are awful. They make you use Java style method, even though you're writing C#. And they are prescribing an inheritance structure (diagram) but the code itself doesn't use that structure. And they make you use inappropriate types.

You've introduced an struct Account even though the requirements do not mention using such a structure.

Here's how it possibly should be written:

abstract class Person
{
public string Name { get; private set; }
public DateOnly Dob { get; private set; }
public string Account { get; private set; }
public Person(string name, DateOnly dob, string account)
{
this.Name = name;
this.Dob = dob;
this.Account = account;
}
public override string ToString() =>
String.Join(Environment.NewLine,
new []
{
$"Name: {this.Name}",
$"Date of birth: {this.Dob.ToShortDateString()}",
$"Account Type: {this.Account}",
});
}
class Employee : Person
{
public Employee(string name, DateOnly dob, string account)
: base(name, dob, account) { }
}
class Customer : Person
{
public Customer(string name, DateOnly dob, string account)
: base(name, dob, account) { }
}
static void Main(string[] args)
{
Employee em = new Employee("L", new DateOnly(1999, 11, 11), "Savings");
Console.WriteLine("Employee details:");
Console.WriteLine(em.ToString());
}

huangapple
  • 本文由 发表于 2023年3月1日 12:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定