circle

函数的嵌套

孤人 提交于 2019-11-26 17:36:18
函数嵌套的定义 函数内部定义的函数,无法在函数外部使用内部定义的函数 def f1(): def f2(): print('from f2') f2() f2() ## NameError: name 'f2' is not defined def f1(): def f2(): print('from f2') f2() f1() #from f2 定义一个circle函数,通过传参的形式得到园的面积或者周长 import cmath def circle(r,action): if action=='area': return cmath.pi*r**2 elif action=='length': return 2*cmath.pi*r area=circle(3,'area') print(area) length=circle(3,'length') print(length) 28.274333882308138 18.84955592153876 import cmath def circle(r, action): def area(): return cmath.pi * r ** 2 def length(): return 2*cmath.pi*r if action=='area': return area() else: return length()

c++类的基本形式(一个简单类的简单sample,命名空间)

吃可爱长大的小学妹 提交于 2019-11-26 14:07:27
有人把类说成是占用固定大小内存块的别名,其定义时不占用空间 #include<iostream> #include<string> using namespace std; class mycoach { public: string name="陈培昌"; int age=22; private: string favorite = "和丁大锅在一起"; public: void introduce() { cout << "大家好,我是" + name << "爱好是:" + favorite << endl; } }; void main() { mycoach cpc; cout << "大家好,我是"+cpc.name<<endl; cpc.introduce(); getchar(); } 输出结果: 常见错误----为什么成员函数很重要 #include<iostream> #include<math.h> using namespace std; class mycircle { public: double radius; double mypie = 3.1415926; double s = radius*radius*mypie; }; int main() { mycircle circle; cout << "请输入半径:"; cin >> circle

装饰器模式

只谈情不闲聊 提交于 2019-11-26 11:10:23
装饰器模式 这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。 1、创建一个抽象类 1 public interface Shape { 2 void draw(); 3 } Shape 2、编写抽象类的实现类 1 public class Circle implements Shape { 2 @Override 3 public void draw() { 4 System.out.println("Circle"); 5 } 6 } Circle 1 public class Rectangle implements Shape { 2 @Override 3 public void draw() { 4 System.out.println("Rectangle"); 5 } 6 } Rectangle 3、创建实现了 Shape 接口的抽象装饰类 1 public abstract class ShapeDecorator implements Shape{ 2 protected Shape decoratedShape; 3 4 public ShapeDecorator (Shape decoratedShape){ 5 this.decoratedShape = decoratedShape; 6 } 7 8