zoukankan      html  css  js  c++  java
  • c#类(class)

    类的定义是以关键字class开始的,后面跟类的名称,类的主题包含一个花括号里,下面是类定义的一般格式。

    <access specifier> class class_name
        {
            // member variables
            <access specifier> <data type> variables1;
            <access specifier> <data type> variables2;
            //member method
            <access specifier> <return Type> method1(parameter_list)
            {
                //method body
            }
        }

    请注意:

    • 访问标识符<access specifier>指定了对类及其成员的访问规则。如果没有指定则使用默认的访问标识符,类的默认访问标识符是internal,类成员的默认访问标识符是private。
    • 数据类型<data type>指定了变量的类型,返回类型<return type>定义了返回方法的返回的数值类型。
    • 如果访问里面的成员,需要用(.)运算符
    • 点运算符链接了对象的名称和成员的名称
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace test_unityc
    {
     
    
        class Box
        {
            public double length;//长度
            public double breadth;//宽度
            public double height;//高度
        }
    
        
        class Box_tester
        {
            static void Main(string[] args)
            {
                Box box1 = new Box();//声明box1,类型为Box
                Box box2 = new Box();//声明box2,类型为Box
                double volume = 0.0f;//体积
    
                //box1详述
                box1.height = 5.0;
                box1.length = 6.0;
                box1.breadth = 7.0;
    
                //box2详述
                box2.breadth = 10.0;
                box2.length = 12.0;
                box2.height = 13.0;
    
                //box1的体积
                volume = box1.height * box1.breadth * box1.length;
                Console.WriteLine("box1的体积{0}", volume);
    
                //box1的体积
                volume = box2.height * box2.breadth * box2.length;
                Console.WriteLine("box2的体积{0}", volume);
    
                Console.ReadKey();
    
            }
        }
    }

     当上面的代码执行时,它会出现如下效果:

    box1的体积210
    box2的体积1560 

     成员函数和封装

    类的成员函数是一个在类的定义中有它的定义或原型的函数,就像其他的变量一样。作为类的一个成员,它能在类的任意对象上操作,且能访问该对象类的所有成员。

    成员变量是类的属性(从设计角度),且它们保持私有来实现封装。这些变量只能使用公共成员函数来访问。

    现在用使用上面的概念来设置并获取一个类当中不同类成员的值:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace test_unityc
    {
    
    
        class Box
        {
            private double length;//长度
            private double breadth;//宽度
            private double height;//高度
            public void setLength(double len)
            {
                length = len;
    
            }
            public void setBreadth(double bre)
            {
                breadth = bre;
    
            }
            public void setHeight(double hei)
            {
                height = hei;
            }
            public double getVolume()
            {
                return length * breadth * height;
            }
        }
    
        
        class Box_tester
        {
            static void Main(string[] args)
            {
                Box box1 = new Box();//声明box1,类型为Box
                Box box2 = new Box();//声明box2,类型为Box
                double volume;//体积
    
                //box1详述
                box1.setHeight(5.0);
                box1.setLength(6.0);
                box1.setBreadth(7.0);
    
                //box2详述
                box2.setHeight(12);
                box2.setLength(13);
                box2.setBreadth(14);
    
                //box1的体积
                volume = box1.getVolume();
                Console.WriteLine("box1的体积{0}", volume);
    
                //box1的体积
                volume = box2.getVolume();
                Console.WriteLine("box2的体积{0}", volume);
    
                Console.ReadKey();
    
            }
        }
    }

     运行结果:

    box1的体积210
    box2的体积2184

     C#中的构造函数

    类的构造函数是类的一个特殊的成员函数,当创建类的新对象是执行。

    构造函数的函数名称与类名称完全相同,且它没有任何返回类型。

    下面的实例说明了构造函数的概念:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace week2._1
    {
        class Program
        {
            private double line; // 线条的长度
            public Program()
            {
                Console.Write("对象已经创建");
            }
    
            public void setLength(double len)
            {
                line = len;
            }
            public double getLength()
            {
                return line;
            }
            static void Main(string[] args)
            {
                Program line = new Program();
                //设置线条长度
                line.setLength(6.0);
                Console.Write("线条的长度为{0}", line.getLength());
                Console.ReadKey();
            }
        }
    }

     结果为:

    对象已经创建线条的长度为6

     默认的构造函数没有参数,但是如果需要可以带参数,这种构造参数叫做参数化构造参数,这种对象可以帮助你在创建对象的同时给对象赋初始值。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace week2._1
    {
        class Program
        {
            private double line; // 线条的长度
            public Program(double len)//参数化构造参数
            {
                Console.Write("对象已经创建,length = {0}",len);
                line = len;
            }
    
            public void setLength(double len)
            {
                line = len;
            }
            public double getLength()
            {
                return line;
            }
            static void Main(string[] args)
            {
                Program line = new Program(10.0);
                Console.WriteLine("线条的长度为:{0}", line.getLength());
                //设置线条长度
                line.setLength(6.0);
                Console.Write("线条的长度为:{0}", line.getLength());
                Console.ReadKey();
            }
        }
    }

     结果:

    对象已经创建,length = 10
    线条的长度为:10
    线条的长度为:6

     c#中的析构函数

    类当中的析构函数是类当中的一种特殊的成员函数,当类的对象超出范围时执行。

    析构函数的名称是在类名称前面加一个波浪号(~)做前缀,它不返回值,也不带任何参数。

    析构函数用于结束程序(比如关闭文件,释放内存等)之前释放资源,析构函数不能继承或重载。

    下面的实例说明析构函数的概念:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace week2._1
    {
        class Program
        {
            private double line; // 线条的长度
            public Program()//构造参数
            {
                Console.WriteLine("对象已经创建");
            }
            //析构函数
            ~Program()
            {
                Console.WriteLine("对象已经删除");
            }
    
          
            static void Main(string[] args)
            {
           
                Program line = new Program();
                       
            }
        }
    }

     结果:

    对象已经创建
    对象已经删除

    c#的静态成员

    我们可以使用static关键字把类成员定义成静态的,当我们声明一个类成员是静态的时,意味着无论有多少个类的对象被创建,只会有一个改静态成员的副本。

    关键字static意味着类中只有一个该成员的实例,静态变量用于定义变量,因为他们的值可以通过直接调用类而不需要创建类的实例来获取,静态变量可以再成员函数或类的定义外部进行初始化。你也可以在类的内部初始化。(静态变量在外部可以通过  类.变量名 访问 。实例变量通过创建实例对象在外部 对象名.变量名 访问

    下面实例演示了静态变量的的用法:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace week2._1
    {
        public class Test
        {
            public static int num;
            public  int number;
    
            public void addNum()
            {
                num++;
            }
            public int getNum()
            {
                return num;
            }
    
        }
        class Program
        {
          
            static void Main(string[] args)
            {
                
                Test.num = 2;
                Test test1 = new Test();
                Test test2 = new Test();
                test1.number = 1;
                test1.addNum();
                test1.addNum();
                test1.addNum();
                test2.addNum();
                test2.addNum();
                test2.addNum();
                Console.WriteLine(test1.getNum()); 
                Console.WriteLine(test2.getNum());
                Console.ReadLine();
            }
        }
    }

     结果:

    8
    8

     你也可以把一个成员函数声明为static。这样的函数只能访问静态变量。静态函数在对象创建之前就已经存在了。下面的实例演示了静态函数(只能通过类名.方法名调用)的用法:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace week2._1
    {
        public class Test
        {
            public static int num;
            public  int number;
    
            public void addNum()
            {
                num++;
                number--;
            }
            public static int getNum()
            {
                return num;
                return number;//出错
            }
    
        }
        class Program
        {
          
            static void Main(string[] args)
            {
                Test test1 = new Test();
                Test test2 = new Test();
                test1.addNum();
                test1.addNum();
                test1.addNum();
                test2.addNum();
                test2.addNum();
                test2.addNum();
                Console.WriteLine(Test.getNum()); 
                Console.WriteLine(Test.getNum());
                Console.ReadLine();
            }
        }
    }

     结果:

    6
    6

     将类成员函数声明为public static无需实例化即可调用类成员函数

    反之,如果不声明为static,即使和Main方法从属于同一个类,也必须经过实例化

  • 相关阅读:
    WebService-CXF 学习笔记
    Java中对于ClassLoader类加载器 嵌套了深度技术的价值
    WebService学习笔记
    MongoDB学习笔记
    java.io.IOException: java.io.FileNotFoundException: /tmp/tomcat.2457258178644046891.8080/work/Tomcat/localhost/innovate-admin/C:/up/154884318438733213952/sys-error.log (没有那个文件或目录)
    Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime
    Mysql优化
    SI架构设计与实践
    高并发高可用处理大数据量
    虚拟机安装CentOS详细操作
  • 原文地址:https://www.cnblogs.com/jiangzijiang/p/13347212.html
Copyright © 2011-2022 走看看