当前位置:网站首页>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
}
边栏推荐
- Wechat applet taro learning record
- PIP uses image website to solve the problem of slow network speed
- A tunnel to all ports of the server
- Redis批量启停脚本
- Differences between tp3.2 and tp5.0
- What does (+) in Oracle mean
- Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“
- How to clear the console password for s7700 device
- jsutlis
- 链式长取值
猜你喜欢
LwIP learning socket (application)
Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
Worldview satellite remote sensing image data / meter resolution remote sensing image
Unity performance optimization
haproxy+keepalived集群搭建02
haproxy+keepalived搭建01
Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire
A tunnel to all ports of the server
随机推荐
static关键字
Transplantation of freetype Library
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
Quality blog——
Demonstration of plug-in use of ventuz basic series
Huawei switch console password reset, device initialization, default password
Zohocrm deluge function application time verification
idea取消引用顯示效果
MAE
Huawei switch basic configuration (telnet/ssh login)
Shader foundation 01
Huawei switch: configure Telnet, SSH and web access
Generate video using clipout in viz engine
Docker installs MySQL and successfully uses Navicat connection
unity2019_ Input management
[step on the pit series] MySQL failed to modify the root password
CLion-Toolchains are not configured Configure Disable profile问题解决
[at] abc 258G - Triangle 三元組可達-暴力
[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)