当前位置:网站首页>Classes and objects
Classes and objects
2022-07-03 08:05:00 【Luckyᕙ(⇀‸↼‵‵)ᕗ】
One . Classes and objects
1、 Definition
object : It is the abstraction of a group of objects with the same attributes and behaviors in the objective world ;
An object is an entity used to describe an objective thing . Everything is object
people 、 classroom 、 The notebook 、 air
class : Classes are abstract types of objects , Object is a concrete instance of a class
class = attribute ( Member variables ) + operation ( Functions that operate on member variables )
A collection of objects with the same properties and methods
human beings 、 Fruits 、 Notebook class
2、 Format :
[ Modifier ] class Class name { }
Class name definition rules :
1、 The first word must be a letter 、 Underline , The following words can be letters 、 Underline 、 Numbers
2、 All words must be capitalized
Class field : Access modifier + data type + Field name
Class content
[ Modifier ] class Class name
{
attribute ;
Method ;
}
// attribute
char sex = ' male ';
float shenGao = 1.75f;
string a = " Light yellow ";// Hair color
// Method
void Smile()
{
Debug.Log(sex);
print(shenGao);
Debug.Log(a);
}
3、 Object creation :
Class name Object name ( Variable )=new Class name ();
LiJiangTao ljt = newLiJiangTao();
4、 Calls to properties and methods
attribute : An extension to the field
Call properties and methods with spot Operator
LiJiangTao ljt = newLiJiangTao();
print(ljt.sex);// Call the property
ljt.Smile();// Calling method
5、 Method definition :
public void print {int a,int b} {...}
Access modifier return type Method name parameter list
It is generally public
Outside the class, you can access the private members of the class through the public member methods
Return type : data type ( basic 、 quote )、void( No return value )
6、 Method operation :
Method definition :
If the method does not return a value , Then this method must output the result
If the method has a return value , Then this method can not output the result , But there must be a return value
The return value uses the keyword :return
Method call :
Call a method with no return value : Object name . Method name ();
Call a method with a return value : Variable = Object name . Method name ();
// Let's define a triangle class , Define two methods , Find the perimeter and area of a triangle
int a = 3, b = 4, c = 5;// Three sides
// Perimeter method has no return value
void ZhouChang() {
int d=a+b+c;
print(" The circumference of a triangle is :"+d);
}
// The area method has a return value
int MianJi() {
int e = a * b / 2;
return e;
}
1. Methods with parameters
classZhaZhiJi
{
// Juicing method
// Give Party A something to drink
string JiaFangZhaZhi(string shuiGuo) {
string s = " Have a drink "+shuiGuo+" juice ";
return s;
}
static void Main(string[] args)
{
ZhaZhiJi zzj = new ZhaZhiJi();
zzj.ZhaZhi(" Apple ");// The actual parameter : Actual parameters
string st = zzj.JiaFangZhaZhi(" durian ");
Console.WriteLine(st);
Console.ReadKey();
}
// Drink it yourself
void ZhaZhi(string shuiGuo)// Formal parameters : Shape parameter
{
Console.WriteLine(" Have a drink "+shuiGuo+" juice ");
}
}
1. Local variables and member variables
string name ;// Member variables , The system provides a value by default
void Print(){
int age=20;// local variable
Console.WriteLine(age);
Console.WriteLine(name);
}
Different scopes
The scope of a local variable is limited to the methods that define it
The scope of the member variable is visible throughout the class
Different initial values
The system will give the member variable an initial value
The system does not assign initial values to local variables
1.static
The difference between static variables and instance variables :
1、 Static variables have only one memory space in memory , In the process of loading classes, complete the memory allocation of static variables ; Every time a new instance object is created , Different memory will be allocated for instance variables
2、 Static variables can be accessed directly through the class name , Instance variables need to be accessed through various objects
//static: static state
int x = 1;
staticint y = 1;// Static variables 、 Class variables
void Get()
{
x++;
y++;
Console.WriteLine(x + "\t" + y);
}
staticvoid Main(string[] args)
{
Test12 t1 = newTest12();
Console.WriteLine(t1.x + "\t" + Test12.y);
t1.Get();
Test12 t2 = newTest12();
t2.Get();
}
The difference between static method and instance method :
- Static methods can only access static variables ; Instance methods can access instance variables , You can also access static variables
- Static methods in Main Method can be used directly ; The instance method is in Main Method must be called through an object
classTest13
{
int value=10;// Member variables 、 Instance variables
staticint a = 2;// Static variables 、 Class variables
void Print3()// Member method 、 Example method
{
Console.WriteLine(value);
Console.WriteLine(a);
}
staticvoid Print()// Static methods 、 Class method
{
//Console.WriteLine(value);
Console.WriteLine(a);
}
//static void Main(string[] args)
//{
// Test13 t1 = new Test13();
// Print();
// t1.Print3();
// //int a = 10;
// //Print(a);
// //Console.WriteLine(a);
// //Print2(t1);
// //Console.WriteLine(t1.value);
// //t1.Get();
// //t1.Get();
// //t1.Get();
// //t1.value = 10;
// //Test13 t2 = t1;// References to objects
// //t2.value++;
// //Console.WriteLine(t1.value + "\t" + t2.value);
//}
- this
//this: Refers to instances of classes
this Usage of :
1、 When a member variable and a local variable have the same name , If you want to call member variables , use this
2、 When constructors call each other , use this
string name;
int age;
double salary;
public Worker():this(" Du Cheng ", 19, 6000)
{
//name = " Yuan Huabin ";
//age = 18;
//salary = 5000;
//Console.WriteLine(name + "\t" + age + "\t" + salary);
}
//public Worker(string name, int age, double salary):this()
public Worker(string name, int age, double salary)
{
this.name=name;
this.age = age;
this.salary = salary;
Console.WriteLine(name + "\t" + age + "\t" + salary);
}
1. Construction method
Definition :
Methods with the same method name and class name , There is no return type , And there's no void.
features : If there is no writing constructor The system will provide a parameterless construction method by default , The method is empty
If write Will override the system default
effect : Initialize member variables
1. Method overloading
Same class Multiple methods in can have identical Method name of , But there are Different parameter lists , This is called method overloading
Method overload rules :
- The method name must be the same .
- Parameter list must be different .
- Methods can have the same or different return types .
- Just because the return type is different is not enough to be called method overloading .
- References to objects
classTest14 {
int a = 1;
void Get()
{
Console.WriteLine(a);
}
void Get2(Test14 t)// The third kind of quotation
{
t.a++;
}
Test14 Get3(Test14 t)// The fourth kind of quotation
{
t.a++;
return t;
}
staticvoid Main(string[] args)
{
Test14 t = newTest14();
t.a = 2;// First reference
Test14 t2 = t;// The second quotation
//Console.WriteLine(t.a+"\t"+t2.a);// 2 2
//t2.Get();
//t.Get2(t);
//Console.WriteLine(t.a + "\t" + t2.a);// 3 3
Test14 t3 = t.Get3(t);
t3.a++;
Console.WriteLine(t.a + "\t" + t3.a);// 4 4
}
边栏推荐
- Chain length value
- the installer has encountered an unexpected error installing this package
- Unity2019_ Lighting system
- Differences between tp3.2 and tp5.0
- JSON与Object之间转换
- Register keyword
- Client server model
- 什麼是定義?什麼是聲明?它們有何區別?
- 【cocos creator】获取资源uuid
- CLion-Toolchains are not configured Configure Disable profile问题解决
猜你喜欢

Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products

数据的存储

Wechat applet taro learning record

Free use until 2015 -- viz artist multi touch plug-in package

WPF:解决MaterialDesign:DialogHost 无法关闭问题

How to configure GDAL under idea

PostGIS space function

Xlua task list youyou

Haproxy+kept build 01

freetype库的移植
随机推荐
Chain length value
[at] abc 258G - Triangle 三元組可達-暴力
一个实习生的CnosDB之旅
P2704 [noi2001] artillery position (shape pressure DP)
freetype库的移植
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Huawei switch: configure Telnet, SSH and web access
Wechat native applet cloud development learning record 01
【LeetCode】2. Valid parentheses · valid parentheses
How to establish rectangular coordinate system in space
RM delete file
static关键字
JSON与Object之间转换
2020-12-12
Storage of data
[at] abc 258G - Triangle 三元组可达-暴力
Redis view client connection
How to clear the console password for s7700 device
Youyou1 of xlua knapsack system
What does (+) in Oracle mean