It is simple.
In a method or a constructor 'this' refer to the current object.
That is object whose method or constructor is being called.
Manuja with PHP and JAVA |
|
Hello guys.Let us talk about a special key word in Opp.That is 'this'
It is simple. In a method or a constructor 'this' refer to the current object. That is object whose method or constructor is being called.
0 Comments
Good day to all my friends.Today I would like to say a important Opp concept.That is constructor. Why constructors.... Because it is simple and concise. constructor has got following features. 1. It has got the same name as the class name. 2. It has no return data type.Even viod. 3. This is because they return the instance of the class itself. following example will lead to a good way to learn about constructor.Run this and have the output as you do in normal java programe. Read the comments I have given. class studentimp{ String name; studentimp(String name){ //this is the constructor and it has no return data type even void. this.name=name; } String getName(){ return name; } } class student{ public static void main(String args[]){ studentimp student1=new studentimp("Manuja");//This is how we instantiate a constructor. System.out.println(student1.getName());//This will pront the name. } } The below show how to run the program.Save the above code in one java file and run as a normal java file. Output : Here we use the class student to compile as it is the class that consist the main method...
Now it over..Be happy... -Ujitha Manuja Sudasingha- Hi my dear friend today my aim is to show you about the methods in java and how to access them using Opp concepts. Come with me...... Read the comments I have given below and get the idea of opp.Run the code and have the output... Find what are Objects in Opp..Flowing show you about the creating objects in Opp. public class first{ //My class name is first String name; //Here I declare the variable but not assign any value.So it do not use any memory space. void setName(String studentName){ //Here I am create a set method.This method return //nothing.So void data type is used. name=studentName; } String getName(){ //Here I create get method.It return a string. return name; } public static void main(String args[]){ first myfirst=new first(); //This is how we create Objects.This is called instiating. myfirst.setName("Manuja"); System.out.println(myfirst.getName());//This is how we access the getName method using // newly created object. } } Now it is All over..... Ok my dear friends I am going to introduce you the Opp concepts using java as it is easy to opp using java than php. lets go... You need basic knowledge in Java to learn this course.. Now just run this codes as a normal java programe and test as a trail to Opp..Have a fun....
Opp concepts are very valuable to understand the PHP frameworks...So I will show you the basic concepts of OPP. Follow me.......
|
Content
All
|