Object oriented what is a class




















Add the following declaration after the constructor in your BankAccount. Add the following at the beginning of BankAccount. Now, let's correctly compute the Balance. The current balance can be found by summing the values of all transactions. As the code is currently, you can only get the initial balance of the account, so you'll have to update the Balance property. This example shows an important aspect of properties.

You're now computing the balance when another programmer asks for the value. Your computation enumerates all transactions, and provides the sum as the current balance. Next, implement the MakeDeposit and MakeWithdrawal methods.

These methods will enforce the final two rules: that the initial balance must be positive, and that any withdrawal must not create a negative balance. This introduces the concept of exceptions. The standard way of indicating that a method cannot complete its work successfully is to throw an exception.

The type of exception and the message associated with it describe the error. Here, the MakeDeposit method throws an exception if the amount of the deposit is not greater than 0. The MakeWithdrawal method throws an exception if the withdrawal amount is not greater than 0, or if applying the withdrawal results in a negative balance. Add the following code after the declaration of the allTransactions list:.

The throw statement throws an exception. Execution of the current block ends, and control transfers to the first matching catch block found in the call stack. You'll add a catch block to test this code a little later on. The constructor should get one change so that it adds an initial transaction, rather than updating the balance directly. Since you already wrote the MakeDeposit method, call it from your constructor.

The finished constructor should look like this:. Now is a property that returns the current date and time. Test this by adding a few deposits and withdrawals in your Main method, following the code that creates a new BankAccount :.

Next, test that you are catching error conditions by trying to create an account with a negative balance. Add the following code after the preceding code you just added:. You use the try and catch statements to mark a block of code that may throw exceptions and to catch those errors that you expect. You can use the same technique to test the code that throws an exception for a negative balance.

Add the following code at the end of your Main method:. To finish this tutorial, you can write the GetAccountHistory method that creates a string for the transaction history. Add this method to the BankAccount type:. This uses the StringBuilder class to format a string that contains one line for each transaction. You've seen the string formatting code earlier in these tutorials.

That inserts a tab to format the output. If you got stuck, you can see the source for this tutorial in our GitHub repo. You can continue with the object oriented programming tutorial. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services.

Privacy policy. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Prerequisites The tutorial expects that you have a machine set up for local development. Create your application Using a terminal window, create a directory named classes. In this tutorial, the bank account supports this behavior: It has a digit number that uniquely identifies the bank account. It has a string that stores the name or names of the owners. The balance can be retrieved.

It accepts deposits. It accepts withdrawals. The initial balance must be positive. Withdrawals cannot result in a negative balance. Define the bank account type You can start by creating the basics of a class that defines that behavior. Open a new account The first feature to implement is to open a bank account. Create deposits and withdrawals Your bank account class needs to accept deposits and withdrawals to work correctly. Generic; Now, let's correctly compute the Balance.

Test this by adding a few deposits and withdrawals in your Main method, following the code that creates a new BankAccount : account. MakeWithdrawal , DateTime. Now, "Rent payment" ; Console. WriteLine account. Balance ; account. MakeDeposit , DateTime. Now, "Friend paid me back" ; Console. Balance ; Next, test that you are catching error conditions by trying to create an account with a negative balance.

WriteLine "Exception caught creating account with negative balance" ; Console. WriteLine e. WriteLine "Exception caught trying to overdraw" ; Console. Challenge - log all transactions To finish this tutorial, you can write the GetAccountHistory method that creates a string for the transaction history.

Amount; report. Add this line to test it in Program. GetAccountHistory ; Run your program to see the results. Next steps If you got stuck, you can see the source for this tutorial in our GitHub repo. You can learn more about these concepts in these articles: Selection statements Iteration statements Is this page helpful?

Yes No. Any additional feedback? Skip Submit. A class is the blueprint from which individual objects are created. The following Bicycle class is one possible implementation of a bicycle:.

The syntax of the Java programming language will look new to you, but the design of this class is based on the previous discussion of bicycle objects. The fields cadence , speed , and gear represent the object's state, and the methods changeCadence , changeGear , speedUp etc.

You may have noticed that the Bicycle class does not contain a main method. That's because it's not a complete application; it's just the blueprint for bicycles that might be used in an application.

The responsibility of creating and using new Bicycle objects belongs to some other class in your application. Here's a BicycleDemo class that creates two separate Bicycle objects and invokes their methods:.

All rights reserved.



0コメント

  • 1000 / 1000