달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

'JLS'에 해당되는 글 2

  1. 2010.12.09 추상 클래스
  2. 2010.12.08 이해안가던 자바퍼즐러6 Multicast
2010. 12. 9. 16:53

추상 클래스 Study/Java2010. 12. 9. 16:53

한마디로 " 설계서, 공통적인 양식 " 라고 생각하면 될 것 같다.

추상메서드는 선언부만 작성하고 구현부는 남겨둔다.(이러하기 때문에 미완성 메서드)

메서드의 내용이 상속받는 클래스에 따라 달라지기 때문에 실제내용은 상속받는 클래스에서 구현하도록 하는 것이다,

사용방법은 키워드 abstract 해서 쓰면 된다.

ex)

abstract class 클래스 명{
abstract void Test();
}

실제로 예제 정리

import javax.swing.*;
abstract class Work{
abstract void temp(int j);
}
class Mode extends Work{
@Override
void temp(int i) {
if(i<23){
System.out.println("Cold Mode");
}else{
System.out.println("Hot Mode");
}
}
}
public class aircon{
public static void main(String args[]){
String ques = "";
int temp = 26;
Work wk = new Mode();
ques = JOptionPane.showInputDialog("온도를 입력하세요");
temp = Integer.parseInt(ques);
wk.temp(temp);
}
}
}

다음은 JLS 의 abstract class 부분을 보자 (  JLS 8.1.1.1 abstract class 발췌 )


8.1.1.1 abstract Classes
An abstract class is a class that is incomplete, or to be considered incomplete.
Normal classes may have abstract methods (§8.4.3.1, §9.4), that is methods
that are declared but not yet implemented, only if they are abstract classes.
If a normal class that is not abstract contains an abstract method, then a compile-
time error occurs.
Enum types (§8.9) must not be declared abstract; doing so will result in a
compile-time error. It is a compile-time error for an enum type E to have an
abstract method m as a member unless E has one or more enum constants, and all
of E’s enum constants have class bodies that provide concrete implementations of
m. It is a compile-time error for the class body of an enum constant to declare an
abstract method.
A class C has abstract methods if any of the following is true:
• C explicitly contains a declaration of an abstract method (§8.4.3).
• Any of C’s superclasses has an abstract method and C neither declares nor
inherits a method that implements (§8.4.8.1) it.
• A direct superinterface (§8.1.5) of C declares or inherits a method (which is
therefore necessarily abstract) and C neither declares nor inherits a method
that implements it.
In the example:
abstract class Point {
int x = 1, y = 1;
void move(int dx, int dy) {
x += dx;
y += dy;
alert();
}
abstract void alert();
}
abstract class ColoredPoint extends Point {
int color;
}

class SimplePoint extends Point {
void alert() { }
}
a class Point is declared that must be declared abstract, because it contains a
declaration of an abstract method named alert. The subclass of Point named
ColoredPoint inherits the abstract method alert, so it must also be declared
abstract. On the other hand, the subclass of Point named SimplePoint provides
an implementation of alert, so it need not be abstract.
A compile-time error occurs if an attempt is made to create an instance of an
abstract class using a class instance creation expression (§15.9).
Thus, continuing the example just shown, the statement:
Point p = new Point();
would result in a compile-time error; the class Point cannot be instantiated
because it is abstract. However, a Point variable could correctly be initialized
with a reference to any subclass of Point, and the class SimplePoint is not
abstract, so the statement:
Point p = new SimplePoint();
would be correct.
A subclass of an abstract class that is not itself abstract may be instantiated,
resulting in the execution of a constructor for the abstract class and, therefore,
the execution of the field initializers for instance variables of that class. Thus,
in the example just given, instantiation of a SimplePoint causes the default constructor
and field initializers for x and y of Point to be executed.
It is a compile-time error to declare an abstract class type such that it is not
possible to create a subclass that implements all of its abstract methods. This
situation can occur if the class would have as members two abstract methods
that have the same method signature (§8.4.2) but incompatible return types.
As an example, the declarations:
interface Colorable { void setColor(int color); }
abstract class Colored implements Colorable {
abstract int setColor(int color);
}
result in a compile-time error: it would be impossible for any subclass of class
Colored to provide an implementation of a method named setColor, taking one
argument of type int, that can satisfy both abstract method specifications,
because the one in interface Colorable requires the same method to return no
value, while the one in class Colored requires the same method to return a value
of type int (§8.4).
A class type should be declared abstract only if the intent is that subclasses
can be created to complete the implementation. If the intent is simply to prevent
instantiation of a class, the proper way to express this is to declare a constructor
(§8.8.10) of no arguments, make it private, never invoke it, and declare no other
constructors. A class of this form usually contains class methods and variables.
The class Math is an example of a class that cannot be instantiated; its declaration
looks like this:
public final class Math {
private Math() { } // never instantiate this class
. . . declarations of class variables and methods . . .
}
:
Posted by 유쾌한순례자
2010. 12. 8. 11:39

이해안가던 자바퍼즐러6 Multicast Study/Java2010. 12. 8. 11:39

이해 안가서 차례대로 변환해봤다.

..생략..
System.out.println((byte)-1);
System.out.println((char)(byte)-1);
System.out.println((int)(char)(byte)-1);

결과

-1
?
65535

일단 int - > byte 타입은 축소기본타입변환인데.. 하위 8비트를 제외하고 모두 잘라낸다. <JLS 5.1.3>

<JLS 발췌>
5.1.3 Narrowing Primitive Conversions
The following 22 specific conversions on primitive types are called the narrowing
primitive conversions:
• short to byte or char
• char to byte or short
• int to byte, short, or char
• long to byte, short, char, or int
• float to byte, short, char, int, or long
• double to byte, short, char, int, long, or float
Narrowing conversions may lose information about the overall magnitude of a
numeric value and may also lose precision.
A narrowing conversion of a signed integer to an integral type T simply discards
all but the n lowest order bits, where n is the number of bits used to represent
type T. In addition to a possible loss of information about the magnitude of
the numeric value, this may cause the sign of the resulting value to differ from the
sign of the input value.
A narrowing conversion of a char to an integral type T likewise simply discards
all but the n lowest order bits, where n is the number of bits used to represent
type T. In addition to a possible loss of information about the magnitude of
the numeric value, this may cause the resulting value to be a negative number,
even though chars represent 16-bit unsigned integer values.
A narrowing conversion of a floating-point number to an integral type T takes
two steps:
1. In the first step, the floating-point number is converted either to a long, if T is
long, or to an int, if T is byte, short, char, or int, as follows:
◆ If the floating-point number is NaN (§4.2.3), the result of the first step of
the conversion is an int or long 0.
◆ Otherwise, if the floating-point number is not an infinity, the floating-point
value is rounded to an integer value V, rounding toward zero using IEEE
754 round-toward-zero mode (§4.2.3). Then there are two cases:
❖ If T is long, and this integer value can be represented as a long, then the
result of the first step is the long value V.
❖ Otherwise, if this integer value can be represented as an int, then the
result of the first step is the int value V.
◆ Otherwise, one of the following two cases must be true:
CONVERSIONS AND PROMOTIONS Narrowing Primitive Conversions 5.1.3
83
❖ The value must be too small (a negative value of large magnitude or negative
infinity), and the result of the first step is the smallest representable
value of type int or long.
❖ The value must be too large (a positive value of large magnitude or positive
infinity), and the result of the first step is the largest representable
value of type int or long.
2. In the second step:
◆ If T is int or long,the result of the conversion is the result of the first step.
◆ If T is byte, char, or short, the result of the conversion is the result of a
narrowing conversion to type T (§5.1.3) of the result of the first step.
The example:
class Test {
public static void main(String[] args) {
float fmin = Float.NEGATIVE_INFINITY;
float fmax = Float.POSITIVE_INFINITY;
System.out.println("long: " + (long)fmin +
".." + (long)fmax);
System.out.println("int: " + (int)fmin +
".." + (int)fmax);
System.out.println("short: " + (short)fmin +
".." + (short)fmax);
System.out.println("char: " + (int)(char)fmin +
".." + (int)(char)fmax);
System.out.println("byte: " + (byte)fmin +
".." + (byte)fmax);
}
}
produces the output:
long: -9223372036854775808..9223372036854775807
int: -2147483648..2147483647
short: 0..-1
char: 0..65535
byte: 0..-1

The results for char, int, and long are unsurprising, producing the minimum
and maximum representable values of the type.
The results for byte and short lose information about the sign and magnitude
of the numeric values and also lose precision. The results can be understood
by examining the low order bits of the minimum and maximum int. The minimum
int is, in hexadecimal, 0x80000000, and the maximum int is 0x7fffffff.
This explains the short results, which are the low 16 bits of these values, namely,

0x0000 and 0xffff; it explains the char results, which also are the low 16 bits of
these values, namely, '\u0000' and '\uffff'; and it explains the byte results,
which are the low 8 bits of these values, namely, 0x00 and 0xff.
Despite the fact that overflow, underflow, or other loss of information may
occur, narrowing conversions among primitive types never result in a run-time
exception (§11).
Here is a small test program that demonstrates a number of narrowing conversions
that lose information:
class Test {
public static void main(String[] args) {
// A narrowing of int to short loses high bits:
System.out.println("(short)0x12345678==0x" +
Integer.toHexString((short)0x12345678));
// A int value not fitting in byte changes sign and magnitude:
System.out.println("(byte)255==" + (byte)255);
// A float value too big to fit gives largest int value:
System.out.println("(int)1e20f==" + (int)1e20f);
// A NaN converted to int yields zero:
System.out.println("(int)NaN==" + (int)Float.NaN);
// A double value too large for float yields infinity:
System.out.println("(float)-1e100==" + (float)-1e100);
// A double value too small for float underflows to zero:
System.out.println("(float)1e-50==" + (float)1e-50);
}
}
This test program produces the following output:
(short)0x12345678==0x5678
(byte)255==-1
(int)1e20f==2147483647
(int)NaN==0
(float)-1e100==-Infinity
(float)1e-50==0.0

char 타입으로 음의 byte 타입 변환은 불가능..

음 이 예제는 아무리 봐도 핵심이 중요한것 같다 -..-

핵심 : 프로그램을 보는 것만으로는 프로그램이 무엇을 하는지 알 수 없다면 프로그램은 아마도 여러분이 원하는

작업을 하지 않을 것이다.

:
Posted by 유쾌한순례자