• 第二节 8构造函数 简单


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    /* 构造函数
     * 构造函数是用来创建对像的特殊函数,函数名和类名一样,没有返回值,连void都不用
     * 构造函数可以有参数,new对像的时候传递函数参数即可
     * 构造函数可以重载,也就是有我个参数不同的构造函数
     * 如果不指定构造函数,则类有一个默认的无参构造函数
     * 如果指定了构造函数,则不再有默认的无参构造函数,如果需要无参构造函数,则需要自己来写
     */
    namespace _8构造函数
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person p = new Person();
                Person p1 = new Person("xxd");
                Person p2 = new Person("xxd",22);
                Console.WriteLine("年龄:{0} 姓名:{1}", p.Age, p.Name);
                Console.WriteLine("年龄:{0} 姓名:{1}", p1.Age, p1.Name);
                Console.WriteLine("年龄:{0} 姓名:{1}", p2.Age, p2.Name);
                Console.ReadKey();
            }
        }
        class Person 
        {
            public string Name { get; set; }
            public int Age { get; set; }
            //重载构造函数
            public Person() { 
            }
    
            public Person(string name) 
            {
                this.Name = name;
            }
    
            public Person(string name, int age) {
                this.Name = name;
                this.Age = age;
            }
        }
    }
    

      

  • 相关阅读:
    windows update error 0x8024401c
    linux杀毒软件ClamAV的安装使用
    firewalld防火墙设置
    RPM-GPG-KEY详解
    centos修改默认启动级别
    debian9.6修改系统语言
    ubuntu18.04修改网卡名称为eth0
    Windows server 1709(不含UI)模板部署
    NVIDIA-SMI系列命令总结
    bash: lspci: command not found解决方法
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2367337.html
Copyright © 2020-2023  润新知