类的小练习

《高级程序设计语言原理》实 验 报 告 ( 5 )


实验名称:面向对象编程,继承机制


实验地点:信息楼318


所使用的工具软件及环境:VS2013


一、实验目的:

1、面向对象编程语言

2、类定义


二、实验内容:

定义一个名为Box的类,有三个实例变量:length, widh 和height, 同时定义一个设置长方体长、宽、高值的方法setlwh( )和计算长方体面积area ()和体积volumn( )的方法。

主要程序代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace BOX

{

class Box

{

private double length;

private double widh;

private double height;

public void setlwh(double length,double widh,double height)

{

this.length = length;

this.widh = widh;

this.height = height;

}

public void area()

{

Console.WriteLine("面积为");

Console.WriteLine( 2 * (length * widh + widh * height + length * height));

}

public void volumn()

{

Console.WriteLine("体积为");

Console.WriteLine(length * widh * height);

}

}

class program

{

static void Main(string[] args)

{

Box b = new Box();

b.setlwh(1,2,3);

b.area();

b.volumn();

Console.ReadLine();

}

}

}


三、程序运行结果示例

时间: 2024-08-23 22:56:02

类的小练习的相关文章

关于理解python类的小题

今天看了python部落翻译的一篇<一道python类的小题>文章,感觉挺有启发性,记录下来: 1 print('A') 2 class Person(object): 3 print('B') 4 def __int__(self,name): 5 print('C') 6 self.name = name 7 print('D') 8 print('E') 9 10 11 p1= Person('name1') 12 p2 = Person('name2') 输出结果: A B D E C

C# 类使用小demo

太晚了,不说了,直接上图 运行结果 C# 类使用小demo

CSS-用伪类制作小箭头(轮播图的左右切换btn)

先上学习地址:http://www.htmleaf.com/Demo/201610234136.html 作者对轮播图左右按钮的处理方法一改往常,不是简单地用btn.prev+btn.next的图片代替,而是用纯css制作. 第一个是left按钮,即prev: .vmc-arrow-left:after { content: "\e079"; } 第二个是right按钮的,也就是next下一张的按钮: .vmc-arrow-right:after { content: "\e

Java有关多态类的小程序

1.使用instanceof运算符判断一个对象是否可以转换为指定的类型: 代码: public class TestInstanceof { public static void main(String[] args) { //声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类 //但hello变量的实际类型是String Object hello = "Hello"; //String是Object类的子类,所以返回true. Sys

Qt Widgets——动作类与小部件菜单项

本文主要涉及以下三个类: QAction ——QWidgetAction QActionGroup QAction可称为动作类,它一般可当作菜单中的项组成菜单,也可作为工具栏上的按钮,它主要由图标.文本及快捷键三部分组成.QActionGroup用于将QAction分组,设置组内各QAction的互斥性质(exclusive ),设置后,组内的动作,在外观上形成多选框(不互斥)或单选框(互斥).QWidgetAction继承自QAction,它可将自定义的小部件插入到菜单项中,用于QSystem

Eclipse AST 实现一个类信息统计小程序

这个是对Eclipse AST 获取与访问的介绍,http://blog.csdn.net/LoveLion/article/details/19050155  (我的老师的博客) 本文的工具类完全来自于该文章,其他的类有这篇文章的影子 首先是 工具类,不多讲,请看老师的介绍 package com.kyc.rec; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNot

理解使用before,after伪类实现小三角形气泡框

先来理解before和after伪类的用法吧,before从字面上的意思可以理解为前面的意思,它一般和content属性一起使用,把内容插入在其他元素的前面,同理after的含义就是把内容插入到其他元素的后面了.先来看一个简单的demo,如下代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> &

python类定义小问题

1 >>> class Athlete: 2 def __init__ (self,a_name,a_dob=None,a_times=[]) : 3 self.name = a_name 4 self.dob = a_dob 5 self.times = a_times 6 7 8 >>> sarah =Athlete('Sarah Sweeney','2002-6-27',['2.65','2:56','1.45']) 9 >>> james =

类继承小总结

类继承的功能:1,在已有类的基础上添加功能.2, 可以给类增加数据.eg:对于一个表示人的类,可以派生一个类,添加人是否可以飞行这个数据成员(bool canfly:)3,可以修改类方法的行为. 成员初始化语法:对于以下的类,可以有两种定义构造函数的方法: 1 class TableTennisPlayer 2 { 3 private: 4 string firstname; 5 string lastname; 6 bool hasTable; 7 public: 8 TableTennisP