Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Computer science paper, Essays (high school) of Computer science

basic class and object in c sharp

Typology: Essays (high school)

2020/2021

Uploaded on 03/03/2022

unknown user
unknown user 🇬🇧

8 documents

1 / 15

Toggle sidebar

Related documents


Partial preview of the text

Download Computer science paper and more Essays (high school) Computer science in PDF only on Docsity! 12/04/2019 1 Basic Class and Object in C# Week 2 Objects and Classes The objectives of this chapter are: To discuss important aspects of the software development process To define objects and classes To understand object models and class models To explain instance variables and instance methods To explain constructors 12/04/2019 2 One of the primary features of the O-O paradigm is its ability to manage complexity. Clear identification of system entities (objects) Abstract classification of system entities (classes) Clear delineation of entity boundaries (roles, responsibilities, encapsulation, cohesion, coupling) Clear separation of Abstraction boundaries (realistic class hierarchies) Less ambiguous mapping between the real world and the software components modelling the real world. (objects) Unfortunately, the O-O paradigm is not clearly understood Many programmers simply think that creating classes is OO programming Many programmers erroneously think that because they use a language which supports OO concepts they are implementing OO designs Many programmers, when confronted with code, cannot identify whether it is object-oriented and, if so, what elements make it OO. The Object Oriented Paradigm Creating Classes Objects in the object model are formalized Objects are abstracted into classes Only attributes and methods relevant to our domain are classified. Attributes are formalized into instance variables Behaviour is formalized into methods Classes are represented on a class diagram Object interaction is also abstracted Associations are identified Added to class diagram Classes and the class diagram represent the static structure of the system How the system behaves is not represented by this model. 12/04/2019 5 Encapsulation Encapsulation is a very important O-O concept Each object has 2 views. An internal view and an external view Encapsulation is a form of protection Also called Information Hiding The outside world does not have direct access to the internal implementation or representation of an object As long as the external view does not change, the internal view can take on any form without affecting the outside world By hiding data and providing methods to gain access to it, an object can maintain high data integrity Methods have the responsibility of maintaining data integrity private visibility offers full encapsulation protected and default offer limited encapsulation public offers no encapsulation public class Account { private int number; private int overdraftLimit; private Date startDate; private String owner; [... methods ...] } Encapsulation Example Class Definition: Instances: number = 11346 overdraftLimit = 1000 startDate owner Fred Jones number = 12364 overdraftLimit = 300 startDate owner Billy Wiggs Instance variables are encapsulated. - no direct access from outside the object Each object has its own variables. These variables are declared within the class. May 1, 2001 June 15, 1994 12/04/2019 6 Defining Instance Methods Method definitions include a method signature and a method body. Methods signatures are defined with the following syntax: The return type can be: a fundamental data type an object reference void (no return) Parameters are optional If the method takes no parameters, empty brackets are required () Multiple parameters are separated by commas Parameters are defined by type and name A parameter is a local variable whose scope is the method. modifier return_type method_name(type name, ...) Defining Instance Methods - Visibility Methods have the same visibility modifiers as variables public - the method can be invoked from anywhere private - the method can only be invoked from within the class protected - the method can be invoked directly from within the class, within the package, or from within any subclass. default (no modifier specified) - the method can be invoked directly from within the package If a method is part of the class's public interface (external view), the method should be public If a method is part of the class's internal implementation (ie, support method, etc), it should be private. Be careful using default or protected. Use only when justified. 12/04/2019 7 Defining Instance Methods - Body A method's body contains all the statements to be executed as part of the method The method body is contained within curly braces after the method definition: Use {} placement and indentation to clearly show code structure public class CalculationSheet { public void performCalculations() { [... method body ...] } public void clearSheet() { } [...] } Returning values from methods A method which has a non-void return type MUST return a value The return value's type must match the type defined in the method's signature. A void method can use a return statement (with no return value) to exit the method. The return value can be used the same as any other expression. public class Car { private int currentGear; private int currentRpms; public int calculateSpeed() { return currentRpms * currentGear; } } 12/04/2019 10 Invoking Instance Methods To invoke a method on an object, use the . (dot) operator • If there is a return value, it can be used as an expression objectReference.methodName(parameters); Car aCar = new Car(); [...] if (aCar.calculateSpeed()>110) { System.out.println("You're Speeding!"); } [...] Passing Parameters to Methods Method parameters are declared in the method's signature. When a method invocation is made, any parameters included in the invocation are passed to the method All parameters are passed by value. Ie, a copy is made The value of fundamental data types are copied The value of object references (ie memory addresses) are copied • Parameters become variables within the method. They are not known outside the method. public float calculateInterestForMonth(float rate) { return lowBalanceForMonth * (rate/12.0); } 12/04/2019 11 Overloading Methods Java allows for method overloading. A Method is overloaded when the class provides several implementations of the same method, but with different parameters The methods have the same name The methods have differing numbers of parameters or different types of parameters The return type MUST be the same public float calculateInterestForMonth() { return lowBalanceForMonth * (defaultRate/12.0); } public float calculateInterestForMonth(float rate) { return lowBalanceForMonth * (rate/12.0); } Accessor Methods - gets Objects have variables. Because of encapsulation, those variables are generally private However, the outside world may need to use those variables The class implementor may choose to add a "get" method to return the value The usual name of the get method is the name of the variable prefixed with the word "get" getName(), getAddress(), getPhone(), getBalance() public class BankAccount { private float balance; public float getBalance() { return balance; } 12/04/2019 12 Accessor Methods - sets Similarly, the outside world may need to set the value of an instance variable The class implementor may choose to implement a set method. The responsibility of the set method is to set the appropriate variable WHILST MAINTAINING data integrity of the object. The usual name of the set method is the name of the variable prefixed with the word "set" setName(), setAddress(), setPhone(), setBalance() public class BankAccount { private String ownerName; public void setOwnerName(String aName) { ownerName = aName; } Initializing Objects - Constructors When an object is created, all instance variables are initialized to the default value for their type Fundamentals are 0, 0.0, '\000' or false Object references are null In order to put the object into a usable state, its instance variables should be initialized to usable values This could be accomplished by calling the various set methods This is not always possible because it is not required that all instance variables have set methods. C# provides for another method of initializing objects When an object is created, a constructor is invoked. The responsibility of the constructor method is to initialize the object into a usable state.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved