使用构造函数

938人阅读 评论(0)

public class Taxi
{
public bool isInitialized;
public Taxi()
{
isInitialized = true;
}
}

class TestTaxi
{
static void Main()
{
Taxi t = new Taxi();
System.Console.WriteLine(t.isInitialized);
}
}

class NLog
{
// Private Constructor:
private NLog() { }

public static double e = System.Math.E;  //2.71828...
}

int i = new int();
Console.WriteLine(i);

int i;
Console.WriteLine(i);

int a = 44;  // Initialize the value type...
int b;
b = 33;      // Or assign it before using it.
Console.WriteLine("{0}, {1}", a, b);

public class Employee
{
public int salary;

public Employee(int annualSalary)
{
salary = annualSalary;
}

public Employee(int weeklySalary, int numberOfWeeks)
{
salary = weeklySalary * numberOfWeeks;
}
}

Employee e1 = new Employee(30000);
Employee e2 = new Employee(500, 52);

public class Manager : Employee
{
public Manager(int annualSalary)
: base(annualSalary)
{
}
}

public Manager(int initialdata)
{
}

public Manager(int initialdata) : base()
{
}

public Employee(int weeklySalary, int numberOfWeeks)
: this(weeklySalary * numberOfWeeks)
{
}

public Employee(int annualSalary)
{
salary = annualSalary;
}

class CoOrds
{
public int x, y;

// constructor
public CoOrds()
{
x = 0;
y = 0;
}
}

// A constructor with two arguments:
public CoOrds(int x, int y)
{
this.x = x;
this.y = y;
}

CoOrds p1 = new CoOrds();
CoOrds p2 = new CoOrds(5, 3);

class Circle : Shape
{
{
}
}

class CoOrds
{
public int x, y;

// Default constructor:
public CoOrds()
{
x = 0;
y = 0;
}

// A constructor with two arguments:
public CoOrds(int x, int y)
{
this.x = x;
this.y = y;
}

// Override the ToString method:
public override string ToString()
{
return (System.String.Format("({0},{1})", x, y));
}
}

class MainClass
{
static void Main()
{
CoOrds p1 = new CoOrds();
CoOrds p2 = new CoOrds(5, 3);

// Display the results using the overriden ToString method:
System.Console.WriteLine("CoOrds #1 at {0}", p1);
System.Console.WriteLine("CoOrds #2 at {0}", p2);
}
}

CoOrds #1 at (0,0)

CoOrds #2 at (5,3)

public class Person
{
public int age;
public string name;
}

class TestPerson
{
static void Main()
{
Person p = new Person();

System.Console.Write("Name: {0}, Age: {1}", p.name, p.age);
}
}

Name: , Age: 0

abstract class Shape
{
public const double pi = System.Math.PI;
protected double x, y;

public Shape(double x, double y)
{
this.x = x;
this.y = y;
}

public abstract double Area();
}

class Circle : Shape
{
{
}
public override double Area()
{
return pi * x * x;
}
}

class Cylinder : Circle
{
{
y = height;
}

public override double Area()
{
return (2 * base.Area()) + (2 * pi * x * y);
}
}

class TestShapes
{
static void Main()
{
double height = 3.0;

Cylinder tube = new Cylinder(radius, height);

System.Console.WriteLine("Area of the circle = {0:F2}", ring.Area());
System.Console.WriteLine("Area of the cylinder = {0:F2}", tube.Area());
}
}

Area of the circle = 19.63

Area of the cylinder = 86.39

class NLog
{
// Private Constructor:
private NLog() { }

public static double e = System.Math.E;  //2.71828...
}

public class Counter
{
private Counter() { }
public static int currentCount;
public static int IncrementCount()
{
return ++currentCount;
}
}

class TestCounter
{
static void Main()
{
// If you uncomment the following statement, it will generate
// an error because the constructor is inaccessible:
// Counter aCounter = new Counter();   // Error

Counter.currentCount = 100;
Counter.IncrementCount();
System.Console.WriteLine("New count: {0}", Counter.currentCount);
}
}

New count: 101

// Counter aCounter = new Counter();   // Error

class SimpleClass
{
// Static constructor
static SimpleClass()
{
//...
}
}

public class Bus
{
// Static constructor:
static Bus()
{
System.Console.WriteLine("The static constructor invoked.");
}

public static void Drive()
{
System.Console.WriteLine("The Drive method invoked.");
}
}

class TestBus
{
static void Main()
{
Bus.Drive();
}
}

The static constructor invoked.

The Drive method invoked.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：27650次
• 积分：368
• 等级：
• 排名：千里之外
• 原创：6篇
• 转载：12篇
• 译文：0篇
• 评论：4条
文章分类
评论排行