我以为开学第一天讲不了课了,大伙儿说说假期,谈点对未来一年有什么想法,有一个什么样的规划,所以我一点上课的准备也没有,可是上了半天课,令我庆幸的是老师带着我们复习,更让我高兴的是我挺投入的也听懂了。
讲的类,构造方法,静态的构造方法,静态的字段,静态的类,部分类,匿名类……
小知识点:
1.用static可以定义静态的字段,静态的方法,静态的类
2.静态方法中只能操作静态字段或者其他静态方法
3.Main()中调用静态方法:类名.方法名()
4.在一个静态的方法中,不可以调用非静态的方法,不能返回非静态的字段
5.default constructor 默认的构造方法,一旦自己定义了构造方法,系统默认的构造方法就不起作用了
6.const定义的常量,在默认情况下就是static静态的。
1创建Person类并添加相应的字段与方法和构造函数,并且有num字段能够记录这个类生生成了多少个具体的对象。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lianxi1
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person("赵一","男",20);
p1.sleep();
Console.WriteLine(Person.num);
Person p2 = new Person("钱二","女", 21);
p2.sleep();
Console.WriteLine(Person.num);
Person p3 = new Person("李四","男", 22);
p3.sleep();
Console.WriteLine(Person.num);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lianxi1
{
class Person
{
private string Name;
private string Sex;
private int Age;
public static int num;
public Person(string name,string sex,int age)
{
Name = name;
Sex = sex;
Age = age;
num++;
}
public void sleep()
{
Console.WriteLine("姓名:{0},性别:{2},年龄:{1}",Name,Age,Sex);
}
}
}
2分别使用面向过程的方法和面向对象的方法,完成如下命题:求 两点之间的距离。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 练习
{
class Program
{
static void Main(string[] args)
{
//两直线间的距离
/*
double distance;
int x2 = int.Parse(Console.ReadLine());
int y2 = int.Parse(Console.ReadLine());
int xdiff = x2 - x1;
int ydiff = y2 - y1;
distance = Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
Console.WriteLine(distance);*/
int x2=int.Parse(Console.ReadLine());
int y2=int.Parse(Console.ReadLine());
Point p1 = new Point();
Point p2 = new Point(x2, y2);
double distance=p1.Distance(p2);
double distance1 = p2.Distance(p1);
Console.WriteLine(distance);
Console.WriteLine(distance1);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 练习
{
class Point
{
public double distance;
private int x, y;
public Point()
{
x = -1;
y = -1;
}
public Point(int h,int z)
{
x = h;
y = z;
}
public double Distance(Point p)
{
int xdiff=x-p.x;
int ydiff=y-p.y;
return distance = Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
}
}
}
3.计算圆面积,创建Circle类,用count字段能够记录这个类生生成了多少个具体的对象。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 练习
{
class Program
{
static void Main(string[] args)
{
Circle c1= new Circle();
double area1 = c1.Area();
Console.WriteLine(area1);
Console.WriteLine(Circle.count);
Circle c2 = new Circle(20);
double area2 = c2.Area();
Console.WriteLine(area2);
Console.WriteLine(Circle.count);
Circle c3 = new Circle(30);
double area3 = c3.Area();
Console.WriteLine(area3);
Console.WriteLine(Circle.count);
Circle c4 = new Circle();
double area4 = c4.Area();
Console.WriteLine(area4);
Console.WriteLine(Circle.GetCircleCount());
Circle c5 = new Circle(20);
double area5 = c5.Area();
Console.WriteLine(area5);
Console.WriteLine(Circle.GetCircleCount());
Circle c6 = new Circle(30);
double area6 = c6.Area();
Console.WriteLine(area6);
Console.WriteLine(Circle.GetCircleCount());
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 练习
{
class Circle
{
public int radius;
public static int count;
public static int GetCircleCount()
{
return count;
}
public Circle()
{
radius = 10;
count++;
}
public Circle(int initialRadius)
{
radius = initialRadius;
count++;
}
public double Area()
{
return Math.PI * radius * radius;
}
}
}
4.匿名类:
var myAnonymouseObject = new { Name = "Michael", Age = 4 };
Console.WriteLine("name:{0},age{1}", myAnonymouseObject.Name, myAnonymouseObject.Age);
var anotherAnonymouseObject = new { Name = "Tom", Age = 45 };
myAnonymouseObject = anotherAnonymouseObject;
Console.WriteLine("name:{0},age{1}", myAnonymouseObject.Name, myAnonymouseObject.Age);
5.部分类:
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PartClassDemo
{
class Program
{
static void Main(string[] args)
{
Circle c = new Circle();
double area1 = c.Area();
Console.WriteLine(area1);
Circle c2 = new Circle(5);
double area2 = c2.Area();
Console.WriteLine(area2);
Console.ReadKey();
}
}
}
Circle1类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PartClassDemo
{
partial class Circle
{
public Circle()
{
radius=0;
}
public Circle(int initialRadius)
{
this.radius = initialRadius;
}
}
}
Circle2类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PartClassDemo
{
partial class Circle
{
private int radius;
public double Area()
{
return Math.PI * radius * radius;
}
}
}