控制台之匿名类,类部分,常量,构造函数等
来源:互联网 发布:室内灯光设计软件 编辑:程序博客网 时间:2024/05/17 20:34
创建控制台
类的封装、分开写类、静态、常量、构造函数,还有类的实例化。
1创建Person类并添加相应的字段与方法和构造函数,并且有num字段能够记录这个类生生成了多少个具体的对象。
2分别使用面向过程的方法和面向对象的方法,完成如下命题:求 两点之间的距离。
1、
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _9._3
{
class Program
{
static void Main(string[] args)
{
Person a = new Person();
Console.WriteLine("name:"+a.Name+" age:"+a.Age);
a.jishu();
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _9._3
{
class Person
{
string name;
public string Name
{
get { return name; }
}
int age;
public int Age
{
get { return age; }
}
public static int num;
public Person()
{
name = "匿名";
age = 0;
num++;
}
public Person(string name, int age)
{
this.name = name;
this.age = age;
num++;
}
public void jishu()
{
Console.WriteLine(num);
}
}
}
2、
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _09._03
{
class Program
{
static void Main(string[] args)
{
int x1=-1, x2, y1=-1, y2;
x2 = int.Parse(Console.ReadLine());
y2 = int.Parse(Console.ReadLine());
double h1 = Math.Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
Console.WriteLine(h1);
Class1 a = new Class1();
Class1 b = new Class1(x2,y2);
Console.WriteLine(a.jishu(b));
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _09._03
{
class Class1
{
int x, y;
public Class1()
{
x = -1;
y = -1;
}
public Class1(int x,int y)
{
this.x = x;
this.y = y;
}
public double jishu(Class1 p)
{
return Math.Sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
}
}
匿名类和类的分开写
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _3
{
class Program
{
static void Main(string[] args)
{
var a = new { Naem = "靠", Age = 23 };
//匿名类
Console.WriteLine("Name:"+a.Naem+" Age:"+a.Age);
Class1 b = new Class1();
b.ab();
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _3
{
partial class Class1
{
int a;
int b;
public Class1()
{
a = 1;
b = 2;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _3
{
partial class Class1
{
public void ab()
{
Console.WriteLine(a + b);
}
}
}
- 控制台之匿名类,类部分,常量,构造函数等
- 黑马程序员 Java基础<一>---> 面向对象与类之概述(匿名对象、封装、构造函数、this、静态等)
- Java基础<五>---> 面向对象与类(匿名对象、构造函数、this、静态等)
- java-常量、匿名类
- 使用匿名类的构造函数
- 匿名类的构造函数很特殊
- 解决匿名类构造函数问题 Java
- 使用匿名类的构造函数
- 匿名类的构造函数很特殊
- 部分类、匿名类
- C#控制台 输出hello world之 用类的实例构造函数输出
- C#控制台 输出hello world之 用类的静态构造函数输出
- 构造函数 匿名类 一个类分开写
- C#控制台 关于派生类构造函数的注意事项
- 黑马程序员C#学习在路上--类成员之字段、常量、属性、方法、构造函数、析构函数
- C++类,构造函数,修饰符等
- net学习之类与对象、new关键字、构造函数、常量和只读变量、枚举、结构、垃圾回收、静态成员、静态类等
- Android之PackageManagerService构造函数部分分析
- BufferedReader中文乱码解决
- 在路上
- 浅谈时下的互联网营销
- android系统学习笔记一
- 时间工具类[DateUtil]
- 控制台之匿名类,类部分,常量,构造函数等
- P3P实现第三方Cookie写入的问题
- 用ffmpeg把H264数据流解码成YUV420P
- Java操作MongoDB-作者:hoojo
- 若爱 就如桃花般地去爱
- 对程序的理解(1)
- 被开水烫了怎么办
- ubuntu安装雅黑字体
- 修改weblogic 10 JVM