KHO THƯ VIỆN 🔎

Thinking in C# phần 3

➤  Gửi thông báo lỗi    ⚠️ Báo cáo tài liệu vi phạm

Loại tài liệu:     PDF
Số trang:         104 Trang
Tài liệu:           ✅  ĐÃ ĐƯỢC PHÊ DUYỆT
 













Nội dung chi tiết: Thinking in C# phần 3

Thinking in C# phần 3

using System;// Demonstration of a simple constructor.public class Rock2 (public Rock2(int i> { // This is the constructorConsole.WriteLine ("Creating

Thinking in C# phần 3 g Rock number: " + i);11public class Simpleconstructor {public static void Main() {for (int 1 0; 1 < 10; Ì++)new Rock2(i);}}///:-Constructor argument

s provide you with a way to provide parameters for the initialization of an object For example, if the class Tree has a constructor that takes a singl Thinking in C# phần 3

e integer argument denoting the height of the tree, you would create a Tree object like this:Tree t - new Tree(12);// 12-foot treeIf Tree(int) is your

Thinking in C# phần 3

only constructor, then the compiler won't let you create a Tree object any other way.Constructors eliminate a large class of problems and make the co

using System;// Demonstration of a simple constructor.public class Rock2 (public Rock2(int i> { // This is the constructorConsole.WriteLine ("Creating

Thinking in C# phần 3 ate from definition. In c#, definition and initialization are unified concepts—you can't have one without the otherThe constructor is an unusual type

of method because it has no return value. Tins is distinctly different from a void return value, in which the method is declared explicits- as returni Thinking in C# phần 3

ng nothing With constructors you are not given a choice of what you return; a constructor always returns an object of the constructor’s type. If there

Thinking in C# phần 3

was a declared return value, and if you could select your own, the compiler would somehow need to know what to do with that return value. Accidentall

using System;// Demonstration of a simple constructor.public class Rock2 (public Rock2(int i> { // This is the constructorConsole.WriteLine ("Creating

Thinking in C# phần 3 lling you "member names cannot be the same as their enclosing type."Chapters: initialization à CleanupMethod overloadingOne of the important features

in any programming language is the use of names. When you create an object, you give a name to a region of storage. A method is a name for an action. Thinking in C# phần 3

By using names to describe your system, you create a program that is easier for people to understand and change It's a lot like writing prose—the goal

Thinking in C# phần 3

is to communicate with your readers.You refer to all objects and methods by using names. Well-chosen names make it easier for you and others to under

using System;// Demonstration of a simple constructor.public class Rock2 (public Rock2(int i> { // This is the constructorConsole.WriteLine ("Creating

Thinking in C# phần 3 mber of different meanings—it's overloaded. This is useful, especially when it comes to trivial differences. You say “wash the shirt," “wash the car,"

and “wash the dog.” It would be silly to be forced to say, "shirtWash the shirt," “carWash the car," and “dogWash the dog" just so the listener doesn Thinking in C# phần 3

't need to make any distinction about the action performed. Most human languages are redundant, so even if you miss a few words, you can still determi

Thinking in C# phần 3

ne the meaning We don't need unique identifiers—we can deduce meaning from context.Most programming languages (C 111 particular) require you to have a

using System;// Demonstration of a simple constructor.public class Rock2 (public Rock2(int i> { // This is the constructorConsole.WriteLine ("Creating

Thinking in C# phần 3 floats—each function requires a unique name.In o and other languages in the C++ family, another factor forces the overloading of method names; the co

nstructor. Because the constructor’s name is predetermined by the name of the class, there can be only one constructor name. But what if you want to c Thinking in C# phần 3

reate an object in more than one way? For example, suppose you build a class that can initialize itself in a standard way or by reading information fr

Thinking in C# phần 3

om a file. You need two constructors, one that takes no arguments (the default constructor, also called the no-arg constructor), and one that takes a

using System;// Demonstration of a simple constructor.public class Rock2 (public Rock2(int i> { // This is the constructorConsole.WriteLine ("Creating

Thinking in C# phần 3 ame of the class. Thus, method overloading is essential to allow the same method name to be used with different argument types And although method ove

rloading is a must for constructors, it's a general convenience and can be used with any method.Here’s an example that shows both overloaded construct Thinking in C# phần 3

ors and overloaded ordinary methods:Ỉ52Thinking in c#www.MindView.net//:c05:OverLoading.es// Demonstration OỈ both constructor // and ordinary method

Thinking in C# phần 3

overloading, using System;class Tree { ini height;public Tree 0 (Prt("Pl anti ng a seedling”); height - 0;}

using System;// Demonstration of a simple constructor.public class Rock2 (public Rock2(int i> { // This is the constructorConsole.WriteLine ("Creating

using System;// Demonstration of a simple constructor.public class Rock2 (public Rock2(int i> { // This is the constructorConsole.WriteLine ("Creating

Gọi ngay
Chat zalo
Facebook