Learning Unity 3D game development daily notes (C # the third week)

Learning Unity 3D game development daily notes (C # the third week)

on Monday on Tuesday on Wednesday Thursday Friday
Objects (classes, attributes, methods (functions)) for Static: static Sealed interface Object-oriented summary
Constructor Static class Virtual methods Display implements the interface
Destructor Singleton Abstract methods and classes Knowledge Point
The difference between the structures and classes: Summary inherit Selecting virtual methods and abstract class Indexer
Protection class fields (attributes) What is a global variable? Operator overloading
What is a local variable?

2019.7.22 (a)

Object-oriented:

Class: is an abstract description of the same attributes and behavior, is a comprehensive description of a particular group of
Properties: The specific features of the object
Method (function): This object is owned by behavior

Constructor: a constructor function for a class object; it is the start of an object, to open up the heap area;

  1. When object initialization configuration of the object constructor overloads by direct methods, new new constructor must be called, and the name of the constructor must match a class name, no return type;
  2. When we did not write a class constructor, the system will default to add a no-argument constructor; if there have arg constructor class, then the system will not be added to a default no-argument constructor;
  3. This constructor In another use:
    Here Insert Picture Description

Destructor:

This method is performed by the destruction of the object, we generally do not write, the system will automatically add. When garbage collection system call, the system will call the destructor memory recovery region;
Here Insert Picture Description

Summary: The difference between the structures and classes:

  1. A time when the object type structure, a class object reference type;
  2. If there is no class constructor, adding a default constructor with no arguments, parameters if there is configured, then the default constructor with no arguments will not be added;
    structure can not add their own constructor with no arguments are added by default, if the parameters have been configured, the system or will add a constructor with no arguments;
  3. Structure with a reference current constructor requires complete structure of all attributes assigned; class does not;
  4. The class attributes may be assigned a default value, the structure can not be assigned default values;

Class field protection:

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Get: get the meaning is returned to the calling return the corresponding value at the get method,
Set: is the value assigned to the external value is the value of Age, we can once again set the screening method to select whether or not assigned to age;

2019.7.23 (b)

Static: static:

①静态类型的变量是类最先加载到内存区域的;(静态方法不可以调用非静态方法,但是非晶态方法可以调用静态方法);
②静态变量或者方法,不是对象所单独拥有的;是所有对象共有的;通过类名、属性或者方法直接访问;
③静态区域的内容,是整个类中最后销毁的,也就是说不能乱声明静态属性或方法;
Here Insert Picture Description
Here Insert Picture Description

静态类:

单例 ---- 用于管理类,比如GameManager;
静态类 ---- 用于工具类,写工具给其他开发者调用的,比如系统提供的Console.ReadLine();
  1. 静态类在项目一运行就加载到内存;
  2. 静态类声明周期最长,所以整个项目中,能不写静态类就不写;
  3. 静态类是整个项目所共享的,静态类方法在整个项目中都可以通过类名调用;

单例:

什么类需要单例?–这个类不能有多个对象,比如游戏管理类;

Here Insert Picture Description

特点:

①整个项目中这个类只有一个对象;
②单例类的写法:把构造函数私有化;

继承:

  1. object类是C#中所有类的父类;

  2. 访问修饰符
    子类名 : 父类名
    {

    }
    ①一个类只能继承一个类(一个类只能有一个直接父类);
    ②子类保存父类的所有方法和属性;
    ③base 和 this关键字在构造函数中的不同作用;
    ④派生类的构造函数需要指向一个父类的构造,如果不指定将会指向父类默认(无参)构造

  3. 访问修饰符 :
    ①public 公共的 ,所有类中了可以访问
    ②protected 保护类型的 ,在本类和子类中可以访问
    ③private 私有的,只有在本类中可以访问;类中声明变量默认是private
    ④Internal: 程序集中可以使用;

  4. new关键字,如果在子类中声明的一个成员,与父类中重名,将默认启用new关键字,将会为这个新的成员申请一个新的空间,而父类中的成员将会被隐藏。

  5. 里氏替换:is as 的用法;
    Here Insert Picture Description

什么叫全局变量?

变量时类的一部分,有效范围是整个类,类中的所有方法都可以访问;即使声明在类的最后一行,在方法里也可以使用;

什么叫局部变量?

在一个固定的代码块中声明的变量;作用域是当前声明的地方到代码块结束,声明之前不能用;一般类对象临时使用,可以使用代码块括号括起来,减小类对象作用域也就是减少了对象的生命周期,提高代码效率。

2019.7.24(三)

1.Sealed:

修饰类时,当前类就不能被继承(封闭类);
修饰虚方法时,子类不能重写该方法;
Here Insert Picture Description

2.虚方法:

①在父类中使用virtual修饰方法,子类中使用Override对其重写;
②子类重写了父类的虚方法,new一个子类对象赋值给父类声明一个变量;
当我们调用p2的speak虚方法,起始调用的是Chinese中重写的speak方法;
③new 关键字覆盖 和 virtual override 区别:
New关键字覆盖的方法,什么类的对象调用什么类的方法
Override调用的是生成对象时最后重写的方法;
④完成基类虚方法子类重写条件:方法名一致,返回值类型一致,参数类型个

virtual override:
Here Insert Picture Description
new:
Here Insert Picture Description

3.抽象方法和抽象类:

① use abstract modification must be in an abstract class;
② abstract method is no specific method body;
③ abstract class also need to modify abstract, subclass inherits the abstract class must implement all abstract methods abstract class;
④ although abstract class you can not have instances of objects, but you can also write constructor, sub-class constructor with no difference between the ordinary rules subclass;

4. The abstract class virtual methods and selection:

① need to be instantiated if the parent class, this time with the need to instantiate a virtual method, using an ordinary class;
② If the parent class is instantiated not required, may be considered by the abstract class;
③ a mixture generally added using an abstract class virtual method, sub a class can be selectively implemented using a virtual method, the method must be implemented by the abstract methods;

2019.7.25 (d)

interface

Here Insert Picture Description
1. Use the interface keyword modification, methods and properties of the interface must not have a method body;
2. Interfaces can not have constructors, to know that he is not a class, of course, can not have a class object interfaces;
3. Default members are public;
4 The class can implement multiple interfaces, an interface can inherit an interface, when a class inherits from another class and implements the interface to the base class must EDITORIAL;
Here Insert Picture DescriptionHere Insert Picture Description

Display implements the interface

Here Insert Picture Description
result:
Here Insert Picture Description

Knowledge points:

	引用类型默认值是null;
	值类型默认值是0;---------每一种编程语言不一样;

Indexer:

Here Insert Picture Description

namespace 索引器
{

    class Person
    {
        public string name;
        public int age;
        public string sex;
        public string address;
        public float height;

        public Person() { }
        public Person(string name,int age,string sex,string address,float height)
        {
            this.name = name;
            this.age = age;
            this.sex = sex;
            this.address = address;
            this.height = height; 
        }

        public string this[int x] {
            get {
                switch (x)
                {
                    case 0:
                        return name;
                    case 1:
                        return age.ToString();
                    case 2:
                        return sex;
                    case 3:
                        return address;
                    case 4:
                        return height.ToString();
                    default:
                        return "下标号越界";
                }
            }
            set {
                switch (x)
                {
                    case 0:
                        name = value;
                        break;
                    case 1:
                        age = int.Parse( value);
                        break;
                    case 2:
                        sex = value;
                        break;
                    case 3:
                        address = value;
                        break;
                    case 4:
                        height = float.Parse(value);
                        break;
                    default:
                        break;
                }
            }

        }

        public  void SayHello()
        {
            Console.WriteLine("哈罗,我是" + name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            
            Person zhangsan = new Person("张三", 20, "男", "中国", 1.75f);
            zhangsan[1] = "34";
            Console.WriteLine(zhangsan[1]);

            Console.ReadLine();
        }
    }
}

Overloaded operators:

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

2019.7.26 (e)

Object-oriented summary:

  1. Package: how to write a class? What properties, fields, methods,
  2. Inheritance: Richter replacement (is as); improved code efficiency; base
  3. Polymorphism: the same methods applied to different objects have different effects;
    1. Virtual function: virtual virtual function can be written in the general category, it may be written in an abstract class; subclasses can override the virtual method in the parent class, may not be rewritten;
    2. Abstract classes, abstract functions: abstract abstract function must be written in an abstract class, subclass must override, abstract classes can not have objects, abstract function no function body
    3. Interface: is not a class, the class prefer supplement; A class can implement multiple interfaces (interfaces using it particularly flexible i)

Guess you like

Origin blog.csdn.net/weixin_43814599/article/details/97173365
Recommended