zoukankan      html  css  js  c++  java
  • 构造函数


    1,类的初始化函数,用来创建对象,可以用来对对象进行初始化

    函数名与类名一样,无返回值,连void都不需要

    可以有参数,可以重载,不同参数个数的函数重载

    同样个数的参数,但类型不一样,也能成功,调用时按照格式

     

    2.没有自己写构造函数的时候系统自动分配一个无输入参数的构造函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
        }
    }
     
    class Person
    {
        public Person()//类似于系统自建
        { }
    }

    3.一旦建立了一个构造函数,则系统自带的无参数的构造函数即被自动删除,如下代码会报错

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class Program
    {
        static void Main(string[] args)
        {
     
            Person p1 = new Person();//报错
        }
    }
     
    class Person
    {
        public Person(int a)//报错
        { }
    }

    4.同样个数的参数,但类型不一样,也能成功,调用时按照格式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
      
    namespace 构造函数1
    {
        class Program
        {
            static void Main(string[] args)
            {
      
                Person p1 = new Person();
                Person p2 = new Person("tom");
                Person p3 = new Person("lili",18);
                Person p4 = new Person(19, "dd");
                Console.WriteLine("名字是{0},年龄是{1}",p1.Name,p1.Age);
                Console.WriteLine("名字是{0},年龄是{1}", p2.Name, p2.Age);
                Console.WriteLine("名字是{0},年龄是{1}", p3.Name, p3.Age);
                Console.WriteLine("名字是{0},年龄是{1}", p4.Name, p4.Age);
      
                IncAge(p1);
                Console.WriteLine("名字是{0},年龄是{1}", p1.Name, p1.Age);
      
                //关于对象的引用
      
      
                Console.ReadKey();
            }
      
            static void IncAge(Person pp)//类似指针
            { pp.Age += 1; }
        }
      
        class Person
        {
            public string Name { get; set; }//属性
            public int Age { get; set; }
      
            public Person()
            {
                Name = "未命名";
                Age = 0;
            }
            public Person(string name)
            {
                Name = name;
                Age = 0;
            }
            public Person(string name, int age)//同样个数的参数,但类型不一样,也能成功,调用时按照格式
            {
                Name = name;
                Age = age;
            }
            public Person(int age, string name)
            {
                Name = name;
                Age = age;
            }
        }
      
    }

  • 相关阅读:
    斯特林数及斯特林反演
    关于斯特林数的应用总结
    向量运算与几何意义
    linux shell——md5sum,sha1sum,sort,uniq (转)
    hudson配置教程
    linux下安装tomcat和部署web应用
    Tomcat Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
    《Ant权威指南》笔记(一)
    ant调用shell命令(Ubuntu)
    hudson--ant编写记录
  • 原文地址:https://www.cnblogs.com/zongzi10010/p/4183622.html
Copyright © 2011-2022 走看看