zoukankan      html  css  js  c++  java
  • C#和.Ne学习第九天

    1、类
    语法:
    [public] class 类名
    {
    字段;
    属性;
    方法;
    }
    写好了一个类之后,我们需要创建这个类的对象,
    那么,我们管创建这个类的对象过程称之为类的实例化。
    使用关键字 new.

    this:表示当前这个类的对象。
    类是不占内存的,而对象是占内存的。


    2、属性
    属性的作用就是保护字段、对字段的赋值和取值进行限定。
    属性的本质就是两个方法,一个叫get()一个叫set()。
    既有get()也有set()我们诚之为可读可写属性。
    只有get()没有set()我们称之为只读属性
    没有get()只有set()我们称之为只写属性


    3、访问修饰符
    public:公开的公共的,在哪都能访问。
    private:私有的,只能在当前类的内部进行访问,出了这个类就访问不到了。

    4、
    当我们创建好一个类的对象后,需要给这个对象的每个属性去赋值。
    我们管这个过程称之为对象的初始化。

    5、静态方法和非静态方法的区别
    1)、在非静态类中,既可以有实例成员,也可以有静态成员。
    2)、在调用实例(非静态)成员的时候,只能使用对象名.实例(非静态)成员;
    在调用静态成员的时候,只能使用类名.静态成员名;
    总结:静态成员必须使用类名去调用,而实例成员(非静态)使用对象名调用。
    静态函数中,只能访问静态成员,不允许访问实例(非静态)成员。
    实例函数中,既可以使用静态成员,也可以使用实例(非静态)成员。
    静态类中只允许有静态成员,不允许出现实例成员。

    使用:
    1)、如果你想要你的类当做一个"工具类"去使用,这个时候可以考虑将类写成静态的。
    2)、静态类在整个项目中资源共享。
    只有在程序全部结束之后,静态类才会释放资源。


    堆 栈 静态存储区域

    释放资源。GC Garbage Collection垃圾回收器


    6、构造函数
    作用:帮助我们初始化对象(给对象的每个属性依次的赋值)
    构造函数是一个特殊的方法:
    1)、构造函数没有返回值,连void也不能写。
    2)、构造函数的名称必须跟类名一样。

    创建对象的时候会执行构造函数
    构造函数是可以有重载的。
    ***
    类当中会有一个默认的无参数的构造函数,当你写一个新的构造函数之后,不管是有参数的还是
    无参数的,那个默认的无参数的构造函数都被干掉了。

    7、new关键字
    Person zsPerson=new Person();
    new帮助我们做了3件事儿:
    1)、在内存中开辟一块空间
    2)、在开辟的空间中创建对象
    3)、调用对象的构造函数进行初始化对象


    8、this关键字
    1)、代表当前类的对象
    2)、在类当中显示的调用本类的构造函数 :this

    实例:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Student
    10     {
    11         private string _name;
    12         private int _age;
    13         private double _china;
    14         private double _math;
    15         private double _english;
    16 
    17         #region 类成员属性
    18         public double English
    19         {
    20             get { return _english; }
    21             set { _english = value; }
    22         }
    23         public double Math
    24         {
    25             get { return _math; }
    26             set { _math = value; }
    27         }
    28         public double China
    29         {
    30             get { return _china; }
    31             set { _china = value; }
    32         }
    33         public int Age
    34         {
    35             get { return _age; }
    36             set { _age = value; }
    37         }
    38         public string Name
    39         {
    40             get { return _name; }
    41             set { _name = value; }
    42         }
    43         #endregion
    44         public Student(string name, int age, double china, double math, double english)
    45         {
    46             this.Name = name;
    47             this.Age = age;
    48             this.China = china;
    49             this.Math = math;
    50             this.English = english;
    51         }
    52         public Student(string name, int age):this(name, age, 0, 0, 0)
    53         {
    54         }
    55         public void ShowStudent()
    56         {
    57             Console.WriteLine("姓名:{0}年龄:{1}语言:{2}数学:{3}英语:{4}",this.Name, this.Age, this.China, this.Math, this.English);
    58         }
    59     }
    60 
    61 }
    View Code

    练习:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 练习2
     8 {
     9     class Ticket
    10     {
    11         private double _distance;   //距离
    12         private double _price;      //价格
    13         public double Distance
    14         {
    15             get { return this._distance; }
    16             set { this._distance= value; }
    17         }
    18         /// <summary>
    19         /// 0-100公里        票价不打折
    20         //101-200公里    总额打9.5折
    21         //201-300公里    总额打9折
    22         //300公里以上    总额打8折
    23         /// </summary>
    24         public double Price
    25         {
    26             get 
    27             {
    28                 if(this.Distance >= 0 && this.Distance <= 100)
    29                 {
    30                     this._price = this.Distance * 1.0;
    31                 }
    32                 else if(this.Distance >= 101 && this.Distance <= 200)
    33                 {
    34                     this._price = this.Distance * 0.95;
    35                 }
    36                 else if(this.Distance >= 201 && this.Distance <= 300)
    37                 {
    38                     this._price = this.Distance * 0.9;
    39                 }
    40                 else
    41                 {
    42                     this._price = this.Distance * 0.8;
    43                 }
    44                 return this._price;
    45             }
    46         }
    47         public Ticket(double distance)
    48         {
    49             if(distance > 0)
    50             {
    51                 this.Distance  = distance;
    52             }
    53             else
    54             {
    55                 this.Distance = 0;
    56             }
    57         }
    58     }
    59 }
    Ticket类
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 练习2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Ticket test = new Ticket(90);
    14             Console.ReadKey();
    15             Console.WriteLine("{0}公里{1}元",test.Distance, test.Price);
    16             Console.ReadKey();
    17         }
    18     }
    19 }
    Main
  • 相关阅读:
    GO开发[一]:golang语言初探
    Python带参数的装饰器
    Python函数篇
    19. logging模块
    18. json模块
    17. os模块
    16. sys模块
    15. random模块
    14. 模块-time模块
    29. java面向对象项目
  • 原文地址:https://www.cnblogs.com/2016Study/p/5514038.html
Copyright © 2011-2022 走看看