- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Parameterized Constructor
- Constructor with parameters is called a parameterized constructor.
- Below classes are examples of parameterized constructors.
Parameterized Constructor:
Explanation of the Below Code:
- We create a class with the init method.
- We create a chirag-named class.
- We create a lastname1-named class variable.
- Then we define the init method with the self and lastname2 parameters, then create an instance variable using the class variable which returns the argument of the lastname2 parameter.
- Then we define a readlastname-named method with a self-parameter, we take the instance variable in the print function which returns the value of instance variable.
- Then we create an a-named instance(object) of the chirag class.
- Then we call the readlastname method using the a-named instance(object).
- We create a chirag-named class.
- Then we define the init method with the self and lastname2 parameters, then create an instance variable using the class variable which returns the argument of the lastname2 parameter.
- Then we define a readlastname-named method with a self-parameter, we take the instance variable in the print function which returns the value of instance variable.
- Then we create an a-named instance(object) of the chirag class.
- Then we call the readlastname method using the a-named instance(object).
Access some specific info. of an object using some specific method:
Explanation of the Below Code:
- We create a class with the init method.
- We create a me-named class.
- Then we define the init method with the self, fname, lname, and age parameters, then create some instance variable for each parameters.
- Then we define a showfname-named method with a self-parameter, we take the instance variable in the print function which returns the value of instance variable.
- Then we define a showlname-named method with a self-parameter, we take the instance variable in the print function which returns the value of instance variable.
- Then we define a showAge-named method with a self-parameter, we take the instance variable in the print function which returns the value of instance variable.
- Then we create the a1 and a2-named instances(objects) with three arguments (separate with a comma) of me class.
- Then we call the showfname method using the a1-named instance(object).
- Then we call the showlname method using the a2-named instance(object).
- Then we call the showAge method using the a2-named instance(object).
- We create a me-named class.
- Then we define the init method with the self, fname, lname, and age parameters, then create some instance variable for each parameters.
- Then we define a showfname-named method with a self-parameter, we take the instance variable in the print function which returns the value of instance variable.
- Then we create the a1 and a2-named instances(objects) with three arguments (separate with a comma) of me class.
- Then we call the showfname method using the a1-named instance(object).
- Get link
- X
- Other Apps
Comments
Post a Comment