Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python基础 #44

Open
lcgyh opened this issue Dec 18, 2017 · 1 comment
Open

python基础 #44

lcgyh opened this issue Dec 18, 2017 · 1 comment
Labels

Comments

@lcgyh
Copy link
Owner

lcgyh commented Dec 18, 2017

No description provided.

@lcgyh lcgyh added the 主题 label Dec 18, 2017
@lcgyh lcgyh closed this as completed Aug 17, 2018
@lcgyh lcgyh reopened this Aug 29, 2018
@lcgyh
Copy link
Owner Author

lcgyh commented Aug 31, 2018

面向对象

class Student():
	name=''
	age=0
	//构造函数
	def __init__(self,name,age):
		self.name=name
		self.age=age
	
	def print(self):
		print('name:'+self.name)
		print('age:'+str(self.age))
	
	//私有方法,只能从内部调用,不能从实例访问
	def __prints(self):
		print('name:'+self.name)
		print('age:'+str(self.age))
	//类方法
	@classmethod
	def plus_sum(cls):
		pass
		
	//静态方法
	@staticmethod
	def add(x,y):
		pass	
		
student=Student("石敢当","20")  //实例化
student.print()  // 调用类下面的方法

备注:

构造函数:当实例化的时候,构造函数自动被调用,当然他也是可以被显式调用

作用:根据实例化时候的参数,初始化类的值


实例变量和类变量

实例变量:和实例对象有关的(传入参数保存的变量,在构造函数中通过self对变量进行保存)

类变量:类下面的变量

通过类去访问变量,获取的是类变量的数据

通过实例化对象去访问变量,获取的是实例变量的数据


实例方法

实例方法:和实例对象(含有self)相关的方法

实例方法中操作类变量:

通过类名直接访问变量:
方法一:Student.name
方法二:self.__class__.name

继承性

@lcgyh lcgyh added the python label Aug 31, 2018
@lcgyh lcgyh changed the title python python基础 Aug 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant