【C#】【平时作业】习题-5-类的基础知识

简介: 【C#】【平时作业】习题-5-类的基础知识

一、概念题

1.举例说明什么是类,什么是对象,并说明类与对象的关系?

类:具有相同特性(数据元素)和行为(功能)的对象的抽象就是类。

对象:对象是人们要进行研究的任何事物,它不仅能表示具体的事物,还能表示抽象的规则、计划或事件。

例如:狗类。大黄狗是狗类的其中一个对象

类与对象的关系就如模具和铸件的关系,类的实例化的结果就是对象,对象的抽象就是类。

类描述了一组有相同特性(属性)和相同行为的对象。

2.什么是类的字段,字段有何作用

字段又称为"成员变量",一般在类的内部做数据交互使用,一般使用private使用

字段的通俗理解:字段就好比我们的个人财产,只供我们自己使用,所以一般是private修饰

作用:限制对象的属性或方法可访问的范围(类的内部,类的外部)

类型: private(私有的:外部不可见) public(共有的:外部可见的)

字段命名规范:字段命名一般采用camel命名法

添加标准:一个类中究竟需要添加几个字段,根据程序编写过程的需要决定

3.举例说明什么是类的属性,属性有何作用?

属性其实是外部访问私有字段的入口,属性本省不保存任何数据

给属性赋值,其实是给属性指向的私有字段赋值

读取属性值,其实是获取属性指向的私有字段值或其它值

作用:在面向对象设计中主要使用属性描述对象静态特征

要求:一般采用Pascal命名法,数据类型和字段一致,使用public修饰

举例

# 字段
private int studentId;
# 属性
public int StudentId
{
get {return student}
set {studentID = value}
}

4.举例说明C#中如何创建类的方法,方法的基本格式是什么?

[修饰符] 返回值类型 方法名 (参数类型 参数, 参数类型 参数)
{
    // 方法体
    //(返回值)
}
/// <summary>
        /// 获取圆的直径
        /// </summary>
        /// <returns></returns>
        public double getDiam() 
        {
            return 2 * _radius;
        }

5.public, private, protect, internal有何区别?

public 关键字是类型和类型成员的访问修饰符。公共访问是允许的最高访问级别,对访问公共成员没有限制。

protected 关键字是一个成员访问修饰符。受保护成员在它的类中可访问并且可由派生类访问。=

private 关键字是一个成员访问修饰符。私有访问是允许的最低访问级别。私有成员只有在声明它们的类和结构体中才是可访问的。

internal 关键字是类型和类型成员的访问修饰符。只有在同一程序集的文件中,内部类型或成员才是可访问的。

6.举例说明,什么是C#程序设计类的封装?

封装:将事物拥有的属性和动作隐藏起来,只保留特定的方法与外界联系。

例如,封装一个Circle类

class Circle
    {
        //字段
        public const double PI = 3.14;
        private double _radius;
        private double _centerX;
        private double _centerY;
        //属性
        public double Radius { get => _radius; set => _radius = (value > 0 && value <= 50) ? value : 50; }
        public double CenterX { get => _centerX; set => _centerX = (value > 0 && value <= 100) ? value : 50; }
        public double CenterY { get => _centerY; set => _centerY = (value > 0 && value <= 100) ? value : 50; }
        //构造函数
        public Circle() 
        {
            ;
        }
        public Circle(double radius) 
        {
            Radius = radius;
        }
        public Circle(double radius, double centerX, double cneterY) 
        {
            Radius = radius;
            CenterX = centerX;
            CenterY = cneterY;
        }
        //功能方法
        /// <summary>
        /// 获取圆的直径
        /// </summary>
        /// <returns></returns>
        public double getDiam() 
        {
            return 2 * _radius;
        }
        /// <summary>
        /// 获取圆的周长
        /// </summary>
        /// <returns></returns>
        public double getPerimeter() 
        {
            return 2 * PI*_radius;
        }
        /// <summary>
        /// 获取圆的面积
        /// </summary>
        /// <returns></returns>
        public double getArea() 
        {
            return PI * _radius * _radius;
        }
    }

二、程序设计

1.设计矩形类,并使用该类

class Rectangle
    {
        private double _length;
        private double _width;
        //构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="length"></param>
        /// <param name="width"></param>
        public Rectangle(double length, double width) {
            _length = length;
            _width = width;
            //纠正赋值非法错误
            if (_length <= 0 || _length > 50) 
            { 
                _length = 50; 
            }
            if (_width <= 0 || _width > 50) 
            {
                _width = 50;
            }
            Checklw();
        }
        //set/get
        public double Length {
            set => _length=(value > 0 && value <= 50) ? value : 50;
            get => _length;
        }
        public double Width
        {
            set => _width = (value > 0 && value <= 50) ? value : 50;
            get => _width;
        }
        //属性getter
        public double getLength() { return _length; }
        public double getWidth() { return _width; }
        //属性setter
        public void setLength(double length) {
            if (length <= 0)
            {
                length = 1;
            }
            else if (length > 50)
            { 
                length = 50; 
            }
            _length = length;
        }
        public void setWidth(double width)
        {
            if (width <= 0)
            {
                width = 1;
            }
            else if (width > 50)
            {
                width = 50;
            }
            _width = width;
        }
        //功能方法
        /// <summary>
        /// 将长方形的长和宽放大一定倍数
        /// </summary>
        /// <param name="factor"></param>
        /// <returns></returns>
        public bool magnify(double factor) 
        {
            Checklw();
            this._length = _length * factor;
            this._width = _width * factor;
            if (this._length == _length * factor && this._width == _width * factor)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }
        //计算长方形面积
        public double ComputeArea() 
        {
            Checklw();
            return this._length * this._width;
        }
        /// <summary>
        /// 计算长方形周长
        /// </summary>
        /// <returns></returns>
        public double ComputePerimeter() 
        {
            Checklw();
            return (this._length + this._width)*2;
        }
        //要求长必须大于款
        public void Checklw() 
        {
            //纠正宽大于长的错误
            if (_length < _width)
            {
                double temp = _length;
                _length = _width;
                _width = temp;
            }
        }
    }
Rectangle cfx = new Rectangle(2222,333);
cfx.setLength(2);
cfx.setWidth(3);
lb1.Text += "长方形长为:"+cfx.getLength()+" | 长方形宽为:"+cfx.getWidth()+"\n";
lb1.Text += "周长为:" + cfx.ComputePerimeter().ToString() + "\n";
lb1.Text +="面积为:" +cfx.ComputeArea().ToString()+"\n";

2.设计圆,并使用该类

//字段
        public const double PI = 3.14;
        private double _radius;
        private double _centerX;
        private double _centerY;
        //属性
        public double Radius { get => _radius; set => _radius = (value > 0 && value <= 50) ? value : 50; }
        public double CenterX { get => _centerX; set => _centerX = (value > 0 && value <= 100) ? value : 50; }
        public double CenterY { get => _centerY; set => _centerY = (value > 0 && value <= 100) ? value : 50; }
        //构造函数
        public Circle() 
        {
            ;
        }
        public Circle(double radius) 
        {
            Radius = radius;
        }
        public Circle(double radius, double centerX, double cneterY) 
        {
            Radius = radius;
            CenterX = centerX;
            CenterY = cneterY;
        }
        //功能方法
        /// <summary>
        /// 获取圆的直径
        /// </summary>
        /// <returns></returns>
        public double getDiam() 
        {
            return 2 * _radius;
        }
        /// <summary>
        /// 获取圆的周长
        /// </summary>
        /// <returns></returns>
        public double getPerimeter() 
        {
            return 2 * PI*_radius;
        }
        /// <summary>
        /// 获取圆的面积
        /// </summary>
        /// <returns></returns>
        public double getArea() 
        {
            return PI * _radius * _radius;
        }.
private void btnCircle_Click(object sender, EventArgs e)
        {
            Circle circle = new Circle();
            circle.Radius = 1;
            lblCircle.Text+= "圆(" + circle.CenterX + "," + circle.CenterY + ") " +
                "半径为" + circle.Radius + " " +
                "直径为" + circle.getDiam() + "\n" +
                "该圆的周长为:" +circle.getPerimeter()+"\n" +
                "该圆的面积为:"+circle.getArea()+"\n";
            lblCircle.Text += "--------------------------------\n";
            circle.Radius = 999;
            circle.CenterX = -999;
            circle.CenterY = 25;
            lblCircle.Text += "圆(" + circle.CenterX + "," + circle.CenterY + ") " +
              "半径为" + circle.Radius + " " +
              "直径为" + circle.getDiam() + "\n" +
              "该圆的周长为:" + circle.getPerimeter() + "\n" +
              "该圆的面积为:" + circle.getArea() + "\n";
        }
相关文章
|
2月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
2月前
|
C#
58.c#:directory类
58.c#:directory类
92 0
|
2月前
|
C#
57.c#:directorylnfo类
57.c#:directorylnfo类
24 0
|
2天前
|
开发框架 .NET 编译器
程序与技术分享:C#基础知识梳理系列三:C#类成员:常量、字段、属性
程序与技术分享:C#基础知识梳理系列三:C#类成员:常量、字段、属性
|
19天前
|
C#
C# 版本的 计时器类 精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出
这篇2010年的文章是从别处搬运过来的,主要包含一个C#类`TimeCount`,该类有多个方法用于处理时间相关的计算。例如,`GetMaxYearCount`计算以毫秒为单位的最大年数,`GetCurrentTimeByMiliSec`将当前时间转换为毫秒,还有`SecondsToYYMMDDhhmmss`将秒数转换为年月日时分秒的字符串。此外,类中还包括一些辅助方法,如处理小数点后保留一位数字的`RemainOneFigureAfterDot`。
|
2天前
|
存储 安全 C#
C# 类的深入指南
C# 类的深入指南
7 0
|
2月前
|
C#
C#的类和对象的概念学习案例刨析
【5月更文挑战第17天】C#是一种面向对象的语言,以类和对象为核心。类作为对象的模板,定义了属性(如Name, Age)和行为(如Greet)。对象是类的实例,可设置属性值。封装通过访问修饰符隐藏实现细节,如Customer类的私有name字段通过Name属性访问。继承允许新类(如Employee)从现有类(Person)继承并扩展。多态让不同对象(如Circle, Square)共享相同接口(Shape),实现抽象方法Area,提供灵活的代码设计。
47 1
|
2月前
|
SQL 存储 Oracle
C# Web控件与数据感应之 Control 类
C# Web控件与数据感应之 Control 类
|
2月前
|
SQL 存储 C#
C# Web控件与数据感应之 TreeView 类
C# Web控件与数据感应之 TreeView 类
|
2月前
|
SQL 存储 Oracle
C# Web控件与数据感应之 CheckBoxList 类
C# Web控件与数据感应之 CheckBoxList 类