Parameter of constructor

Source: Internet
Author: User
Tags constructor

In the example in the previous section, Class A provides a constructor with no parameters and parameters.

Constructors can be without arguments, so initialization of instances of a class is fixed. Sometimes, when we instantiate a class, we need to pass a certain amount of data to initialize the various data, so that initialization is no longer immutable, and then we can use the constructor with parameters to implement different initialization of different instances of the class.

In a constructor with parameters, a class must pass arguments when instantiated, otherwise the constructor is not executed.

Let's review the code example in section 10.2 on the vehicle class. We add the constructor here to verify the passing of parameters in the constructor.

Program Listing 10-6:

Using System;
  Class Vehicle//Definition Auto class {public int wheels;//Only member: Wheel number protected float weight;//protection Member: Weight public Vehicle () {;}
         Public Vehicle (int w,float g) {wheels=w;
    weight=g;
       public void Show () {Console.WriteLine ("The Wheel of Vehicle is:{0}", wheels);
     Console.WriteLine ("The Weight of vehicle is:{0}", weight); Class train//Definition train classes {public int num;//Publicly owned: number of carriages private int passengers;//Private Member: Number of passengers private float weigh T
    Private member: Weight public Train () {;}
      Public Train (int n,int p,float W) {num=n;
      Passengers=p;
    Weight=w;
        public void Show () {Console.WriteLine ("The Num of Train is:{0}", num);
        Console.WriteLine ("The Weight of Train is:{0}", weight);
      Console.WriteLine ("The Passengers train car is:{0}", passengers);
    The class Car:vehicle//defines the saloon type {int passengers;//Private Member: Number of passengers public car (int w,float g,int p): Base (W,G)
          {wheels=w; Weight=g;
        Passengers=p;
        New public void Show () {Console.WriteLine ("The Wheels of Car is:{0}", wheels);
        Console.WriteLine ("The weight of car is:{0}", weight);
      Console.WriteLine ("The passengers of car is:{0}", passengers);
       Class Test {public static void Main () {Vehicle v1=new Vehicle (4,5);
           Train t1=new Train ();
           Train t2=new Train (10,100,100);
           Car c1=new car (4,2,4);
           V1.show ();
           T1.show ();
           T2.show ();
       C1.show (); }
}

The running result of the program is:

The Wheel of vehicle is:0
The weight of vehicle is:0
The NUM of train is:0
The weight of train is:0
The passengers of train is:0
The NUM of train is:0
The weight of train is:0
The passengers of train is:0
The Wheel of car is:4
The weight of car is:2
The passengers of car is:4

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.