Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class Kolokwium1 {
  4. public static void main(String[] args) {
  5.  
  6. A tab[];
  7. System.out.println("Tu wykonują się konktruktory");
  8. A a = new A( 10 );
  9. B b = new B( 20, 3.14f );
  10. C c = new C( 30, 6.28f, 'b' );
  11. System.out.println("koniec wykonywania konstruktorów");
  12. //na początku wywołują się konstruktory
  13. tab = new A[]
  14. {a, b, c};
  15.  
  16.  
  17. System.out.println("show 1");
  18. show( tab );
  19. a.inc();
  20. b.mul( 3.0f );
  21. System.out.println("show 2");
  22. show( tab );
  23.  
  24. c.wrtC = 'd';
  25. System.out.println("show 3");
  26. show( tab );
  27. }
  28.  
  29. public static void show(A tab[])
  30. {
  31. for (int i = 0; i < tab.length; i++)
  32. {
  33. System.out.println( tab[i].toString() );
  34. }
  35. }
  36. }
  37.  
  38. class A
  39. {
  40. int wrtI;
  41.  
  42. public A(int wrtI) {
  43. this.wrtI = wrtI;
  44. System.out.println( "A" + wrtI );
  45. }
  46.  
  47. public void inc() {
  48. wrtI++;
  49. }
  50.  
  51. public String toString() {
  52. return "" + wrtI;
  53. }
  54. }
  55. class B extends A
  56. {
  57.  
  58. float wrtF;
  59.  
  60. public B(int wrtI, float wrtF) {
  61. super( wrtI ); // wywłujemy konstruktor z klasy A
  62. this.wrtF = wrtF;
  63. System.out.println( "B" + wrtF );
  64. }
  65.  
  66. public void mul(int mno) {
  67. wrtF *= mno + 1;
  68. }
  69.  
  70. public void mul(float mno) {
  71. wrtF *= mno; // nie wiem czemu tutaj zwraca 9.42 to jest 3.0 * 3.14
  72. }
  73.  
  74. public String toString() {
  75. return "" + wrtF;
  76. }
  77. }
  78.  
  79. class C extends B
  80. {
  81. char wrtC = 'a';
  82. public C(int wrtI, float wrtF, char wrtC) {
  83. super( wrtI, wrtF );
  84. wrtC = wrtC;
  85. System.out.println( "A" + wrtC );
  86. }
  87.  
  88. public String toString() {
  89. return "" + wrtC;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement