我被困住了,令人沮丧。你能帮忙吗? [英] I've been stuck and it's frustrating. can you please help?

查看:89
本文介绍了我被困住了,令人沮丧。你能帮忙吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *它要求我通过调用第一个构造函数创建一个对象myObj1,并通过调用带有参数Mary Johns和5000.0的第二个构造函数创建一个对象myObj2。你能帮忙吗?/



/*It's asking me to create an object myObj1 by calling the first constructor and create an object myObj2 by calling the second constructor with the arguments "Mary Johns" and 5000.0. Can you help?/

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

namespace Assignment2_Nonyelu
{
    public class A2AccountTest
    {
        public static void Main(string[] args)
        {
            //a
            A2AccountTest myObj1 = new A2AccountTest();
            Console.WriteLine("The balance = {0:C}", ());
            A2Account myObj2 = new A2Account();


        }
    }
    public class A2Account
    {
        private string owner; //Q1
        private double balance;
        private int transaction

    //Q2
    public A2Account()
    {
        owner = "Ify Nonyelu";
        balance = 0;
    }
    public A2Account(string s, double d)
    {
        owner = s;
        balance = d;
    }
    
    //Q3
    public double Balance
    {
            get
            {
                return balance;
            }
            set
            {
                balance = value;
            }
    }
    //Q4
    public double Withdraw(double a)
    {
        if (balance >= a)
        {
            transaction++;
            balance -= a;
            return balance;
        }
        else
        {
            return -1;
        }
            //double amount = a;
            //return balance = 0;
    }
    //Q5
    public string ToString()
    {
            return "Account Owner: Ify Nonyelu: Balance = nnnnn" + balance;
    }
    }
}





我尝试了什么:



我尝试添加Mary Johns和5000.0作为参数,但我不断收到错误。



What I have tried:

I've tried add Mary Johns and 5000.0 as parameters, but I kept getting errors.

推荐答案

这样做



does this

Quote:

A2AccountTest myObj1 = new A2AccountTest();

A2AccountTest myObj1 = new A2AccountTest();





在您的问题中真的有意义吗?你错过在你的问题中提到你要创建一个实例的'对象类型' - 但你对myObj2它是正确的



所以,看看定义对于A2Account类构造函数..你有





really make sense in the context of your question ? You miss mentioning in your question the 'type of object' of which you are to create an instance - yet you have it correct for myObj2

So, look at the definition for A2Account class constructors .. you have

public A2Account()





这是默认构造函数和





which is a default constructor and

public A2Account(string s, double d)





这是一个你可以使用参数的构造函数,是吗?



所以,为什么不...





which is a constructor you can use with parameters, yes ?

so, why not ...

A2Account myObj1 = new A2Account();











and

A2Account myObj2 = new A2Account("Mary Johns", 5000.0);





你能看出它的运作方式吗?有时你只需要退后一步,如果它不工作,吸一口气,尝试新的东西





btw:'我继续犯错误 - 也许是这样,但除非你解释他们实际上是什么,否则无用,否则这里没有人可以帮助你 - 我们无法看到你的屏幕或你的脑袋内



修订版点:类构造函数



can you see how that works ? sometimes you just need to step back from it if its not working, take a breath, and try something new


btw : 'I kept getting errors' - maybe so, but useless unless you explain what they actually were, else no-one here can help you - we cant see your screen or inside your head

revision points for you : class constructors


这篇关于我被困住了,令人沮丧。你能帮忙吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆