Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Door:
- def open(self):
- print 'hello stranger'
- def knock_door:
- a_door = Door()
- Door.open(a_door)
- knock_door()
- employe whatAreYouDoing.
- employee whatIsYourName
- employee whatIsYourDepartmentsName
- // pseudo-code
- function addValues( int x, int y ) return x + y
- // call it
- result = addValues( 8,8 )
- print result // output is 16...
- int addValues( int x, int y )
- {
- return x + y;
- }
- public static int addValues( int x, int y ) {
- return x + y;
- }
- public class Employee {
- Department department;
- String name;
- public String whatsYourName(){
- return this.name;
- }
- public String whatsYourDeparmentsName(){
- return this.department.name();
- }
- public String whatAreYouDoing(){
- return "nothing";
- }
- // Ignore the following, only set here for completness
- public Employee( String name ) {
- this.name = name;
- }
- }
- // Usage sample.
- Employee employee = new Employee( "John" ); // Creates an employee called John
- // If I want to display what is this employee doing I could use its methods.
- // to know it.
- String name = employee.whatIsYourName():
- String doingWhat = employee.whatAreYouDoint();
- // Print the info to the console.
- System.out.printf("Employee %s is doing: %s", name, doingWhat );
- Output:
- Employee John is doing nothing.
- f(x,y) = sin(x) + cos(y)
- Z.g(x) = sin(x) + cos(Z.y)
- result = mySum(num1, num2);
- result = MyCalc.mySum(num1,num2);
- class Example
- {
- public int data = 0; // Each instance of Example holds its internal data. This is a "field", or "member variable".
- public void UpdateData() // .. and manipulates it (This is a method by the way)
- {
- data = data + 1;
- }
- public void PrintData() // This is also a method
- {
- Console.WriteLine(data);
- }
- }
- class Program
- {
- public static void Main()
- {
- Example exampleObject1 = new Example();
- Example exampleObject2 = new Example();
- exampleObject1.UpdateData();
- exampleObject1.UpdateData();
- exampleObject2.UpdateData();
- exampleObject1.PrintData(); // Prints "2"
- exampleObject2.PrintData(); // Prints "1"
- }
- }
- # perfectly normal function
- def hello(greetee):
- print "Hello", greetee
- # generalise a bit (still a function though)
- def greet(greeting, greetee):
- print greeting, greetee
- # hide the greeting behind a layer of abstraction (still a function!)
- def greet_with_greeter(greeter, greetee):
- print greeter.greeting, greetee
- # very simple class we can pass to greet_with_greeter
- class Greeter(object):
- def __init__(self, greeting):
- self.greeting = greeting
- # while we're at it, here's a method that uses self.greeting...
- def greet(self, greetee):
- print self.greeting, greetee
- # save an object of class Greeter for later
- hello_greeter = Greeter("Hello")
- # now all of the following print the same message
- hello("World")
- greet("Hello", "World")
- greet_with_greeter(hello_greeter, "World")
- hello_greeter.greet("World")
- Greeter.greet(hello_greeter, "World")
- Greeter.greet2 = greet_with_greeter
- hello_greeter.greet2("World")
- public void DoSomething() {} // method
- public int DoSomethingAndReturnMeANumber(){} // function
- new Employer().calculateSum( 8, 8 );
- var x = myFunction(4, 3); // Function is called, return value will end up in x
- function myFunction(a, b) {
- return a * b; // Function returns the product of a and b
- }
- var message = "Hello world!";
- var x = message.toUpperCase();
- //Output: HELLO WORLD!
- function person(firstName, lastName, age, eyeColor) {
- this.firstName = firstName;
- this.lastName = lastName;
- this.age = age;
- this.eyeColor = eyeColor;
- this.changeName = function (name) {
- this.lastName = name;
- };
- }
- something.changeName("SomeName"); //This will change 'something' objject's name to 'SomeName'
Add Comment
Please, Sign In to add comment