当前位置:网站首页>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
}
边栏推荐
- [set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
- 一条通往服务器所有端口的隧道
- Multi traveling salesman problem -- overview of formula and solution process
- [usaco12mar]cows in a skyscraper g (state compression DP)
- PIP uses image website to solve the problem of slow network speed
- JS common basic case sorting (continuous update)
- Redis batch startup and shutdown script
- Demonstration of plug-in use of ventuz basic series
- How to configure GDAL under idea
- Idea unreference Display Effect
猜你喜欢
Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“
PostGIS space function
the installer has encountered an unexpected error installing this package
Screenshot tool snipaste
Free use until 2015 -- viz artist multi touch plug-in package
Transfinite hacker cognition
The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
Are you still watching the weather forecast on TV?
Youyou1 of xlua knapsack system
随机推荐
How does yarn link help developers debug NPM packages?
MaxCompute字符串分割函数-SPLIT_PART
[MySQL 12] MySQL 8.0.18 reinitialization
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
E: Unable to locate package ROS melody desktop full
Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products
数据库应用技术课程设计之商城管理系统
Uniapp learning records
Oracle queries grouped by time
*p++、*++p、++*p、(*p)++
Quelle est la définition? Qu'est - ce qu'une déclaration? Quelle est la différence?
Quality blog——
Lua hot update basic grammar
P2704 [NOI2001] 炮兵阵地(状压dp)
I want to do large screen data visualization application feature analysis
oracle中的 (+)是什么意思
PIP uses image website to solve the problem of slow network speed
CLion-Toolchains are not configured Configure Disable profile问题解决