c#笔记2019-01-06

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*2019-01-06C#学习笔记-封装修饰词
* Public 
* Private
*/
namespace Csharp_study
{
//1.public封装修饰词-公共
class TestPublic
{ 
//成员变量height高、width宽
public double height;
public double width;
//成员方法
public double getArea(double height,double width) {
return (height * width);
}
}

//2.Private封装修饰词-私有只能在本类中使用
class TestPrivate 
{
//成员变量-私有
private double Redio;
//成员方法-私有
private double getArea(double canshu)
{
return canshu * canshu * 3.14;
}
//成员方法-公共
public double getAreaPB()
{
//同一类内可以调用private成员变量
Redio = 2.1;
//同一类内可以调用private成员方法
return getArea(Redio);
}
}

class section3
{
static void Main(string[] args){

//TestPublic实例
TestPublic ts = new TestPublic();
//使用TestPublic类的public成员变量
ts.height = 2.1;
ts.width = 3.0;
double Area;
//调用TestPublic类的public成员方法
Area=ts.getArea(ts.height, ts.width);
Console.WriteLine("heigth:{0},width:{1},area:{2}", ts.height, ts.width, Area);

//TestPrivate实例
TestPrivate tp = new TestPrivate();
//tp.Redio = 2.1;//在不同类中无法访问Private声明的成员对象
//tp.getArea(2.1);//在不同类中无法访问Private声明的成员方法
Console.WriteLine("area2:{0}",tp.getAreaPB());


Console.ReadKey();
}
}
}

  

猜你喜欢

转载自www.cnblogs.com/soulsjie/p/10229067.html