14 [Instructor] Java Standards ch07

· Tech· Java
Java

| Reference variable super, constructor super()

Reference variable that points to the object itself; exists only within an instance method // Similar to this

Used to distinguish an ancestor's member from its own member.

class Ex7-2 {

public static void main(String args[ ]) {

Child c = new Child ( );

c.method( );

}

}

class Parent { int x = 10; } //super.x, ancestor method

class Child extends Parent { //this.x

int x = 20;

void method( ) {

System.out.println("x="+x); //near side, this.x

System.out.println("this.x="+this.x);

System.out.println("super.x="+super.x);

}

}

//If there is no separate variable in the child method, the variables of the ancestor method are super.x and this.x

//Can be used the same way


When calling super() ancestor's constructor // this()

class Point {

int x, y;

Point(int x, int y) {

this.x = x;

this.y = y;

}

}

class Point3D extends Point {

int z;

Point3D (int x, int y, int z) {

super(x, y); //When calling the constructor of the ancestor class

this.z = z; //Initialize own members


The constructor must be called in the first line of the constructor.

The compiler automatically types super( );

class Point extends Object {

int x;

int y;

Point ( ) {

this (0, 0);

}

Point(int x, int y) {

super( );

this.x = x;

this.y = y;

}


class Point {

int x;

int y;

Point (int x, int y) {

super( ); //Automatically added by the compiler, all constructors must call another constructor in the first line, a default constructor is required

this.x = x;

this.y = y;

}

String getLocation( ) {

return "x :" + x + "y :" + y;

}

}

class Point3D extends Point {

int z;

Point3D(int x, int y, int z) {

super(x, y);

this.z = z;

}

String getLocation ( ) {

return "x :" + x + ", y :" + y + ", z :" + z;

}


| package, classpath

a group of related classes

Class is a file (*class), package is a folder, rt.jar is a compressed file of classes → changed to module concept

Package declaration //If not present, belongs to unnamed package (default package)

package com.codechobo.book;

Register class path → Environment variables


| import statement, static import statement

The package name can be omitted when using a class.

import java.util. * ; // Can be used without import as a basic package, at least the actual class Date used instead of *

class ImprotTest {

Date today = new Date ( );

}

//java.* is not allowed, Date class is java.sql.Date || Unknown if it is java.util.Date


import static java.lang.Integer.*;

import static java.lang.Math.random;

import static java.lang.System.out;

out.println(random( )); //Allow the omission of the class name

System.out.println(Math.random( ));


| Controller, static, final, abstract

Modifier: An adjective, placed before a class or class member.

Access modifier: public, protected, (default), private // Only 1 allowed, placed at the left end

Others: static, final, abstract, native, transient, synchronized, volatile, strictfp

public static final int WIDTH = 200;

static member variables, methods, class initialization blocks

final class, method, member variable, local variable

final class FinalTest { //final class → String(security), Math(static)

final int Max_SIZE = 10; //Member variable (constant) whose value cannot be changed

final void getMaxSize() { //Method that cannot be overridden (cannot be changed)

final int LV = Max_SIZE; //Local variable (constant) whose value cannot be changed

return Max_SIZE;

}

}

abstract class, method

abstract class AbstractTest { //Abstract class with abstract method

abstract void move ( ); //Abstract method (method without implementation, incomplete)

}

// Instant creation is not possible. Objects can be created only when a complete class is created by inheritance.

AbstractTest a = new AbstractTest( );

Comments

No comments yet. Be the first!

    164 posts in 테크

    15 / 164