完全面向对象的语言意味着所有内容都表示为一个对象, 但是不能区分原始类型和类的对象, 但是C#并非纯粹面向对象, 因为它支持许多过程编程概念, 例如某种程度上的指针。对象是面向对象编程的基本单元, 代表现实生活中的实体。一个典型的C#程序会创建许多对象, 你知道这些对象通过调用方法进行交互。我们可以通过以下方式在C#中创建对象:
1)使用”
new”
运算符:类是引用类型, 并且在运行时, 除非使用new运算符声明该引用类型的任何对象, 否则它将为该对象分配空值。新操作符仅在运行时才将内存中的空间分配给对象, 这意味着分配是动态的。
语法如下:
//The className() is a call
//to the constructor
className ObjectName = new className();
注意:的建设者可以是默认构造函数, 也可以是用户定义的构造函数。
例子:
//C# Program to show the use
//of the new Operator
using System;
namespace NewOperator {class Rectangle {public int length, breadth;
//Parameterized Constructor
//User defined
public Rectangle( int l, int b)
{
length = l;
breadth = b;
}//Method to Calculate Area
//of the rectangle
public int Area()
{
return length * breadth;
}
}//Driver Class
class Program {//Main Method
static void Main( string [] args)
{
//Creating an object using 'new'
//Calling the parameterized constructor
//With parameters 10 and 12
Rectangle rect1 = new Rectangle(10, 12);
//To display are of the Rectangle
int area = rect1.Area();
Console.WriteLine( "The area of the" +
" Rectangle is " + area);
}
}
}
输出如下:
The area of the Rectangle is 120
2)创建对现有对象的引用:只能使用类名和引用名声明引用。该引用不能独立存在。必须将其分配给相同类的现有对象。对引用所做的任何更改都将保存到引用的对象中。这有点像别名。
语法如下:
className RefName;
RefName = objectName;
例子:
//C# Program to show the use
//of references
using System;
namespace Reference {class Triangle {public int side, altitude;
//Not defining a constructor//Method to calculate area
//of the Triangle
public double Area()
{
return ( double )0.5 * side * altitude;
}
}//Driver Class
class Program {//Main Method
static void Main( string [] args)
{
//Creating an object using new
//calls the default constructor
Triangle tri1 = new Triangle();
//Only creates a reference of
//type Triangle
Triangle tri2;
//Displaying area of tri1
Console.WriteLine( "Area of tri1 is "
+ tri1.Area());
//Assigns the reference to tri1
tri2 = tri1;
//Making changes in tri2
tri2.side = 5;
tri2.altitude = 7;
//Displaying area of tri1
//Changes made in the reference tri2
//are reflected in tri1 also
Console.WriteLine( "Area of tri1 is "
+ tri1.Area());
}
}
}
输出如下:
Area of tri1 is 0
Area of tri1 is 17.5
3)创建对象数组:如果需要多个相同类的对象, 则可以创建一个对象数组。这将要求你首先声明数组, 然后初始化每个元素{在这种情况下为object}。你可以使用for循环进行初始化。
语法如下:
className[] arrayName = new className[size];
//C# Program to illustrate how to
//create the array of objects
using System;
namespace ArrayofObjects {class Circle {public int radius;
//Definig Constructor
public Circle()
{
radius = 0;
}//Method to set value of radius
public void setValue( int r)
{
radius = r;
}//Method to calculate the
//area of the Circle
public double Area()
{
return ( double )3.14 * radius * radius;
}
}//Driver Class
class Program {//Main Method
static void Main( string [] args)
{
//To declare an array of two objects
Circle[] circleArray = new Circle[2];
//Initialize the objects
circleArray[0] = new Circle();
circleArray[1] = new Circle();
//to set values for the radius
circleArray[0].setValue(1);
circleArray[1].setValue(2);
//for loop to display areas
for ( int i = 0;
i <
circleArray.Length;
i++)
{
Console.Write( "Area of circle with radius " + (i + 1));
Console.Write( " is " + circleArray[i].Area() + "\n" );
}
}
}
}
【在C#中创建对象的不同方法有哪些()】输出如下:
Area of circle with radius 1 is 3.14
Area of circle with radius 2 is 12.56
推荐阅读
- 在C#中以降序对数组进行排序的不同方法
- 在Golang中连接两个字符串的不同方法
- 在Golang中比较字符串的不同方法
- 第02讲(Flink 入门程序 WordCount 和 SQL 实现)
- <技术向;修正昨天的爬取教程,并介绍一个插件
- <C语言;售货员问题
- 百度APP视频播放中的解码优化
- <还记得雪花吗;用画图深入理解递归
- SpringBoot | 3.1 配置数据源及JDBC #yyds干货盘点#