怎么创建类并且定义函数?

img

4-6可以讲一下嘛谢谢了

【Python代码】

class CLASS(object):
    def __init__(self, specialty,myclass):
        self.specialty = specialty  # 实例属性
        self.myclass = myclass  # 实例属性
    def intro1(self):
        print("我来自{}专业{}班。".format(self.specialty,self.myclass))
my = CLASS("电子信息",2)
my.intro1()

img


【JAVA代码】


public class Test {
    public static void main(String[] args) {
        CLASS my = new CLASS();
        my.specialty = "电子信息";
        my.myclass = 2;
        my.intro1();
    }

}
class CLASS
{
    public String specialty;
    public int myclass;

    public void intro1()
    {
        System.out.println("我来自"+ this.specialty + "专业"+ this.myclass + "班。" );
    }
}

img

4-6中涉及到了python中类的声明,类的参数(属性)设定,类的实例化及赋值,以及类的属性调用

#类的声明
class CLASS():
    #专业
    _specialty=''
    #班级
    _class=''

_c=CLASS #实例
_c._specialty='信息技术'
_c._class='三年二班'

#输出
print('我来自(',_c._specialty,')专业(',_c._class,')班。')