Synchronization of old projects
Part1
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,68 @@
|
||||
namespace Lesson2_成员变量和访问修饰符练习
|
||||
{
|
||||
class Person
|
||||
{
|
||||
public int age,height;
|
||||
public string name,address;
|
||||
}
|
||||
class Student
|
||||
{
|
||||
public string name;
|
||||
public int number, age;
|
||||
public Student deskmate;
|
||||
void Study()
|
||||
{
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
class Cla
|
||||
{
|
||||
public string Major;
|
||||
public int TeacherCapacity,StudentCapacity;
|
||||
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
//Console.WriteLine("Hello, World!");
|
||||
//Person p = new Person();
|
||||
//Person p2 = new Person();
|
||||
//p.age = 10;
|
||||
//p2.age = 20;
|
||||
////不同对象的存储空间不同,所以值不会相同
|
||||
//Console.WriteLine(p.age);
|
||||
//Console.WriteLine(p2.age);
|
||||
|
||||
Person p = new Person();
|
||||
p.age = 10;
|
||||
Person p2 = p;
|
||||
p2.age = 20;
|
||||
//p2指向了p的内存地址,共享一块地址,所以p2改了,p也会改为20
|
||||
Console.WriteLine(p.age);
|
||||
Console.WriteLine(p2.age);
|
||||
|
||||
//Student s = new Student();
|
||||
//s.age = 10;
|
||||
//int age = s.age;
|
||||
//age = 20;
|
||||
////因为age是值类型,是一个新的空间,所以s.age不受影响
|
||||
//Console.WriteLine(s.age);
|
||||
//Console.WriteLine(age);
|
||||
|
||||
Student s = new Student();
|
||||
s.deskmate = new Student();
|
||||
s.deskmate.age = 10;
|
||||
Student s2 = s.deskmate;
|
||||
s2.age = 20;
|
||||
//s.deskmate虽然new了空间在后面被指向了s2的地址
|
||||
//所以s.deskmate的地址是和s2共享的 s.deskmate就是s2
|
||||
//s2.age变了s.deskmate.age也会变
|
||||
Console.WriteLine(s.deskmate.age);
|
||||
Console.WriteLine(s2.age);
|
||||
|
||||
Cla c = new Cla();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user