C # Network application programming basic exercises and Answers (Iv.)

Source: Internet
Author: User
Tags abstract comparison constructor functions integer modifier readline tostring
Programming | network

  1. What are the advantages of object-oriented programming compared to structured programming methods?

Troubleshooting

(1) A process-centric and object-centric comparison

Structured programming approaches are process-centric, and when confronted with a problem, this approach focuses on the hierarchy of problem-solving processes. Object-oriented analysis and design methods focus on objects. Objects have specific behaviors and attributes, and behavior and attributes determine how objects interact with other objects, and how the objects themselves behave.

(2) Comparison of public and hidden data

Structured programming methods simply package data and processes, which are public, or that other code in the program can access the data and processes. An object-oriented implementation hides specific data and exposes only the specific behavior of the object to the user. The code that implements these specific behaviors is not visible to the user, and users can only access these exposed behaviors.

(3) Comparison of single unit and standard unit

Structured programming methods are based on a single unit of code. Object-oriented programming methods allow objects to be independent.

(4) One-time use and reusable comparison

Depending on the implementation, the structured process may not be reused. and an object-oriented approach, an object is a modular unit. Has a complete entity, so it can be highly reusable.

(5) Comparison of ordered algorithm and unordered algorithm

A structured programming method develops a program whose structure is often linear (or top-down). An object-oriented application is a type of program based on message or event-driven. Each object can send messages to other objects. The Windows operating system is such a program.

  2. Briefly answer the following questions.

1 to illustrate how new keywords can be used in those areas?

2 What is the role of the sealed keyword? When do I need to use the SEALED keyword?

3 which keywords can be used for version control?

Troubleshooting

1 in C #, the New keyword can be used as an operator or modifier. Used as an operator to create objects and call constructors on the heap. An inherited member that is used as a modifier to hide a base class member.

2 Use the sealed modifier in a class declaration to prevent other classes from inheriting this class. Using the sealed modifier in a method declaration prevents an extended class from overriding this method.

The sealed modifier is primarily used to prevent unintentional derivation, but it can also cause some run-time optimizations. Specifically, because sealed classes never have any derived classes, calls to virtual function members of instances of sealed classes can be converted to non-virtual calls to handle.

3 override keyword and new keyword are available for version control.

In C #, the method is not virtual by default. To make a method virtual, you must use the virtual modifier in the method declaration of the base class. The derived class can then use the Override keyword to override the virtual methods in the base class, or to use the New keyword to hide the virtual methods in the base class. If neither the override keyword nor the new keyword is specified, the compiler emits a warning and the method in the derived class hides the method in the base class.

  3. Briefly answer the main differences between abstract classes and interfaces.

Troubleshooting

A major difference between an abstract class and an interface is that a class can implement multiple interfaces, but can inherit only from an abstract class or any other type of class.

4. What are the advantages of using a delegate? What are the differences and connections between delegates and events?

Troubleshooting

A delegate in C # is similar to a function pointer in C or C + +. Using delegates enables programmers to encapsulate method references within a delegate object. You can then pass the delegate object to code that can invoke the referenced method without having to know at compile time which method will be invoked. Unlike function pointers in C or C + +, delegates are object-oriented and type-safe.

"Events" in C # is a way for a class to provide notification to customers of that class when something happens. The most common use of events is for graphical user interfaces; Typically, classes that represent controls in the interface have events that are notified when the user makes certain actions, such as clicking a button, on the control.

Use a delegate to declare an event. A delegate object encapsulates a method so that the method can be called anonymously. An event is a method that a class allows a client to provide a delegate for which the methods should be invoked when the event occurs. When an event occurs, the delegate that its customer provides to it is invoked.

  5. Write a console application that completes the following functions and answers the questions raised.

1 Create a Class A, output "a" in the constructor, and then create a class B to output "B" in the constructor.

2 inherits a new class named C from a and creates a member B within C. Do not create constructors for C.

3 creates an object of Class C in the main method, writes out the results of the output after running the program.

4 If you also create a constructor output "C" in C, what is the result of the entire program running?

Troubleshooting

Using System;
public class A
{
Public A ()
{
Console.WriteLine ("A");
}
}
public class B
{
Public B ()
{
Console.WriteLine ("B");
}
}
public class C:a
{
b newb = new B ();
}
Class MainClass
{
public static void Main ()
{
c NEWC = new C ();
Console.ReadLine ();
}
}

Output results:

B

A

If you also create a constructor output "C" in C, add:

Public C ()
{
Console.WriteLine ("C");
}

The result of the entire program running is:

B

A

C

  6. Write a console application that completes the following functions and writes out the output after running the program.

1 Create a Class A, write a method in a that can be overridden with an int type parameter mymethod,

And in this method output the passed integer value plus 10 result.

2) Then create a class B to inherit from Class A, and then rewrite the MyMethod method in a, to connect a

The integer value of the collection is added to 50 and the result is output.

3 in the main method, create the objects of Class A and Class B, respectively, and call the MyMethod method separately.

Troubleshooting

Using System;
public class A
{
public virtual void MyMethod (int num)
{
num + + 10;
Console.WriteLine (num);
}
}
public class B:a
{
public override void MyMethod (int num)
{
Num + + 50;
Console.WriteLine (num);
}
}
Class MainClass
{
public static void Main ()
{
A Newa = new A ();
Newa. MyMethod (2);
b newb = new B ();
Newb. MyMethod (2);
Console.ReadLine ();
}
}

Output results:

12

52

  7. Assume that each node of the nodes class includes two fields: m_data (referencing the node's data) and M_next (referencing the next item in the linked list). Both of these fields are set by the constructor method. The class has two features, the first of which is to access the M_data and M_next fields through a read-only property named data and next. The second feature is an override of the System.Object ToString virtual method. Try to write programs using both class and generic methods to achieve these functions.

Troubleshooting

Using System;
Class Node
{
Object m_data;
Node M_next;
Public node (Object data, node next)
{
m_data = data;
M_next = Next;
}
accessing node data
Public Object Data
{
get {return m_data;}
}
Accessing the next node
Public Node Next
{
get {return m_next;}
}
Get the node data description.
public override String ToString ()
{
Return m_data. ToString ();
}
}
Generic definition of Linked-list node classes
Class Node
{
T m_data;
Node M_next;
Public node (T data, Node next)
{
m_data = data;
M_next = Next;
}
accessing node data
Public T Data
{
get {return m_data;}
set {m_data = value;}
}
Accessing the next node
Public Node Next
{
get {return m_next;}
set {M_next = value;}
}
Get the node data description.
public override String ToString ()
{
Return m_data. ToString ();
}
}
Using a node type or a generic node type
Class LinkedList
{
static void Main (string[] args)
{
Create an integer linked list
Node head = new node (5, NULL);
Head = new Node (ten, head);
Head = new Node (in head);
Traverse the list to find integers and
Int32 sum = 0;
for (Node current = head; current!= null;
Current = current. Next)
//{
Sum + = (Int32) Current. Data;
//}
Output results
Console.WriteLine ("Sum of nodes = {0}", sum);
Create a list of integers with generics
Node head = new node (5, NULL);
Head = new Node (ten, head);
Head = new Node (in head);
Traversal summation
Int32 sum = 0;
for (Node current = head; current!= null;
Current = current. Next)
{
sum = current. Data;
}
Output
Console.WriteLine ("Sum of nodes = {0}", Sum. ToString ());
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.