Guest User

Untitled

a guest
Nov 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. class Door:
  2. def open(self):
  3. print 'hello stranger'
  4.  
  5. def knock_door:
  6. a_door = Door()
  7. Door.open(a_door)
  8.  
  9. knock_door()
  10.  
  11. employe whatAreYouDoing.
  12.  
  13. employee whatIsYourName
  14. employee whatIsYourDepartmentsName
  15.  
  16. // pseudo-code
  17. function addValues( int x, int y ) return x + y
  18. // call it
  19. result = addValues( 8,8 )
  20. print result // output is 16...
  21.  
  22. int addValues( int x, int y )
  23. {
  24. return x + y;
  25. }
  26.  
  27. public static int addValues( int x, int y ) {
  28. return x + y;
  29. }
  30.  
  31. public class Employee {
  32.  
  33. Department department;
  34. String name;
  35.  
  36. public String whatsYourName(){
  37. return this.name;
  38. }
  39. public String whatsYourDeparmentsName(){
  40. return this.department.name();
  41. }
  42. public String whatAreYouDoing(){
  43. return "nothing";
  44. }
  45. // Ignore the following, only set here for completness
  46. public Employee( String name ) {
  47. this.name = name;
  48. }
  49.  
  50. }
  51.  
  52. // Usage sample.
  53. Employee employee = new Employee( "John" ); // Creates an employee called John
  54.  
  55. // If I want to display what is this employee doing I could use its methods.
  56. // to know it.
  57. String name = employee.whatIsYourName():
  58. String doingWhat = employee.whatAreYouDoint();
  59.  
  60. // Print the info to the console.
  61.  
  62. System.out.printf("Employee %s is doing: %s", name, doingWhat );
  63.  
  64. Output:
  65. Employee John is doing nothing.
  66.  
  67. f(x,y) = sin(x) + cos(y)
  68.  
  69. Z.g(x) = sin(x) + cos(Z.y)
  70.  
  71. result = mySum(num1, num2);
  72.  
  73. result = MyCalc.mySum(num1,num2);
  74.  
  75. class Example
  76. {
  77. public int data = 0; // Each instance of Example holds its internal data. This is a "field", or "member variable".
  78.  
  79. public void UpdateData() // .. and manipulates it (This is a method by the way)
  80. {
  81. data = data + 1;
  82. }
  83.  
  84. public void PrintData() // This is also a method
  85. {
  86. Console.WriteLine(data);
  87. }
  88. }
  89.  
  90. class Program
  91. {
  92. public static void Main()
  93. {
  94. Example exampleObject1 = new Example();
  95. Example exampleObject2 = new Example();
  96.  
  97. exampleObject1.UpdateData();
  98. exampleObject1.UpdateData();
  99.  
  100. exampleObject2.UpdateData();
  101.  
  102. exampleObject1.PrintData(); // Prints "2"
  103. exampleObject2.PrintData(); // Prints "1"
  104. }
  105. }
  106.  
  107. # perfectly normal function
  108. def hello(greetee):
  109. print "Hello", greetee
  110.  
  111. # generalise a bit (still a function though)
  112. def greet(greeting, greetee):
  113. print greeting, greetee
  114.  
  115. # hide the greeting behind a layer of abstraction (still a function!)
  116. def greet_with_greeter(greeter, greetee):
  117. print greeter.greeting, greetee
  118.  
  119. # very simple class we can pass to greet_with_greeter
  120. class Greeter(object):
  121. def __init__(self, greeting):
  122. self.greeting = greeting
  123.  
  124. # while we're at it, here's a method that uses self.greeting...
  125. def greet(self, greetee):
  126. print self.greeting, greetee
  127.  
  128. # save an object of class Greeter for later
  129. hello_greeter = Greeter("Hello")
  130.  
  131. # now all of the following print the same message
  132. hello("World")
  133. greet("Hello", "World")
  134. greet_with_greeter(hello_greeter, "World")
  135. hello_greeter.greet("World")
  136.  
  137. Greeter.greet(hello_greeter, "World")
  138.  
  139. Greeter.greet2 = greet_with_greeter
  140. hello_greeter.greet2("World")
  141.  
  142. public void DoSomething() {} // method
  143. public int DoSomethingAndReturnMeANumber(){} // function
  144.  
  145. new Employer().calculateSum( 8, 8 );
  146.  
  147. var x = myFunction(4, 3); // Function is called, return value will end up in x
  148.  
  149. function myFunction(a, b) {
  150. return a * b; // Function returns the product of a and b
  151. }
  152.  
  153. var message = "Hello world!";
  154. var x = message.toUpperCase();
  155. //Output: HELLO WORLD!
  156.  
  157. function person(firstName, lastName, age, eyeColor) {
  158. this.firstName = firstName;
  159. this.lastName = lastName;
  160. this.age = age;
  161. this.eyeColor = eyeColor;
  162. this.changeName = function (name) {
  163. this.lastName = name;
  164. };
  165. }
  166.  
  167. something.changeName("SomeName"); //This will change 'something' objject's name to 'SomeName'
Add Comment
Please, Sign In to add comment