Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- public class Kolokwium1 {
- public static void main(String[] args) {
- A tab[];
- System.out.println("Tu wykonują się konktruktory");
- A a = new A( 10 );
- B b = new B( 20, 3.14f );
- C c = new C( 30, 6.28f, 'b' );
- System.out.println("koniec wykonywania konstruktorów");
- //na początku wywołują się konstruktory
- tab = new A[]
- {a, b, c};
- System.out.println("show 1");
- show( tab );
- a.inc();
- b.mul( 3.0f );
- System.out.println("show 2");
- show( tab );
- c.wrtC = 'd';
- System.out.println("show 3");
- show( tab );
- }
- public static void show(A tab[])
- {
- for (int i = 0; i < tab.length; i++)
- {
- System.out.println( tab[i].toString() );
- }
- }
- }
- class A
- {
- int wrtI;
- public A(int wrtI) {
- this.wrtI = wrtI;
- System.out.println( "A" + wrtI );
- }
- public void inc() {
- wrtI++;
- }
- public String toString() {
- return "" + wrtI;
- }
- }
- class B extends A
- {
- float wrtF;
- public B(int wrtI, float wrtF) {
- super( wrtI ); // wywłujemy konstruktor z klasy A
- this.wrtF = wrtF;
- System.out.println( "B" + wrtF );
- }
- public void mul(int mno) {
- wrtF *= mno + 1;
- }
- public void mul(float mno) {
- wrtF *= mno; // nie wiem czemu tutaj zwraca 9.42 to jest 3.0 * 3.14
- }
- public String toString() {
- return "" + wrtF;
- }
- }
- class C extends B
- {
- char wrtC = 'a';
- public C(int wrtI, float wrtF, char wrtC) {
- super( wrtI, wrtF );
- wrtC = wrtC;
- System.out.println( "A" + wrtC );
- }
- public String toString() {
- return "" + wrtC;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement