C# 类的定义及构造函数的重载
2015-01-24 22:12
288 查看
<span style="font-family:FangSong_GB2312;font-size:18px;"> 1.类的定义及构造函数的重载 代码如下:</span>
<span style="font-family:FangSong_GB2312;font-size:18px;"> </span><pre name="code" class="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _8_4 { class student { string strname; public student(string name) { strname = name; Console.WriteLine("姓名?:" + strname + " 性别: 未知" + " 年龄 : 未知"); } public student(string name,string sex) { strname = name; Console.WriteLine("姓名:" + strname + " 性别: " + sex + " 年龄: 未知"); } public student(string name, string sex,int age) { strname = name; Console.WriteLine("姓名:" + strname + " 性别: " + sex + " 年龄:" + age); } ~student() { Console.WriteLine("学生信息" + strname + "输出完毕"); } } class Program { static void Main(string[] args) { Console.WriteLine("输出学生信息:"); student stu1 = new student("二狗子"); student stu2 = new student("翠花","女"); student stu3 = new student("狗剩","男",30); Console.ReadLine(); } } }
运行结果:
2.方法的重载
代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace p_134{class Program{const double PI = 3.1415926;double getgriph(double r){return 2 * PI * r;}double getgriph(double width,double length){return 2 * (width + length);}static void Main(string[] args){try{Program pro = new Program();Console.WriteLine("计算圆的周长:");double dec_r = Convert.ToDouble(Console.ReadLine());Console.WriteLine("圆的周长为:" + pro.getgriph(dec_r));Console.WriteLine("输入矩形的长和宽(以逗号分隔)");string rectvalue = Console.ReadLine();string[] rectsplit = rectvalue.Split(new char[] { ',' });double dec_length = Convert.ToInt32(rectsplit[0]);double dec_width = Convert.ToInt32(rectsplit[1]);Console.WriteLine("矩形的周长为: " + pro.getgriph(dec_length, dec_width));}catch (Exception e) { Console.WriteLine(e.Message); }Console.ReadLine();}}}运行结果:3.定义一个类,描述一个矩形,包含有长、宽两种属性,有一个无参数构造方法,对长宽赋初值为10,8;一个带两个参数方法,对长宽赋值;一个计算面积方法。编写一个测试类,对以上类进行测试,创建一个长方形,定义其长、宽,输出其面积。<span style="font-family:FangSong_GB2312;font-size:18px;">using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 类与对象{class rect{double rect_length ;double rect_width ;public double Rect_width{get { return rect_width; }set { rect_width = value; }}public double Rect_length{get { return rect_length; }set { rect_length = value; }}public void rect_shape(){rect_length = 10;rect_width = 8;}public double getarea(){return rect_length * rect_width;}static void Main(string[] args){rect shape = new rect();shape.rect_shape();Console.WriteLine("默认矩形的面积:" + shape.getarea());Console.WriteLine( );shape.rect_length = 100;shape.rect_width = 80;Console.WriteLine("矩形的长" + shape.Rect_length);Console.WriteLine("矩形的宽" + shape.Rect_width);Console.WriteLine("矩形的面积:" + shape.getarea());Console.ReadLine();}}}</span>运行结果:
相关文章推荐
- C#构造函数及其函数的重载
- C#重载实例构造函数与构造函数初始化器(转)
- C#中构造函数重载的用法
- C#:方法重载、构造函数、重载构造函数小结
- VS2010 C++ 学习笔记(四) 1.数据的封装 2.类外定义 3.构造函数(无参,有参,重载,初始化列表,拷贝) 4.练习
- c++ 构造函数重定义 + 运算符重载、流重载
- 4.构造方法与重载:定义一个网络用户类,要处理的信息有用户 ID、用户密码、 email 地址。在建立类的实例时把以上三个信息都作为构造函数的参数输入, 其中用户 ID 和用户密码时必须缺省时 ema
- C# 构造函数的重载
- C#基础篇 结构:在结构中定义构造函数
- 构造函数的定义和构造函数的重载
- C#中的重载,重写,构造函数
- C#:重载、重写、隐藏的定义
- c# 类的构造函数的默认定义和个性化定义
- C#中如何定义缺省参数的构造函数
- the constructor in C# (面试题构造函数是否可以继承和重载的解释)
- C#中的构造函数、重写与重载
- 构造函数的定义和构造函数的重载
- 关于C# using 作为定义范围的问题
- C#中构造函数和析构函数的用法
- C#中的函数重载