Java SEオープニング
23378 ワード
一、Java SEオープニング
1.Java基本データ型とそれに対応するパッケージクラス
1.2自動梱包と自動解体
2.Javaパッケージ
3.キーワード
4.コンストラクタ
5.抽象クラス
6.インタフェース
7.多態性
8.内部クラス
9.匿名内部クラス
10.異常処理
1.Java基本データ型とそれに対応するパッケージクラス
* byte Byte
* boolean Boolean
* char Character
* int Integer
* long Long
* short Short
* float Float
* double Double
1.2自動梱包と自動解体
// 1. :
// , :
Integer i = 100;
// :
Integer i = new Integer(100);
// 2. :
// , :
int a = new Integer(100);
// :
int a = new Integer(100).intValue();
2.Javaパッケージ
private √
default √ √
protected √ √ √
public √ √ √ √
:
* 1. private
* 2. getter/setter , public( :boolean getter : is !)
* 3. private, public
3.キーワード
1. static
: static static ; static , !
2. final
: final , final !
3. this
:this static ; this(); , !
4. super 。 super 。 ,Java super();
:super(); !
5. instanceof , :boolean ins = object instanceof class;
:object ,
4.コンストラクタ
, ,
* :public + (){}
* 1. new
* 2. !
* 3. : ,
* 4. , return
* 5.
5.抽象クラス
abstract,
* :
* 1. , !
* 2. , new
* 3. extends
* 4.
* 5. , ,
* 6.
public abstract class Animal {
public static final int MIN_VALUE = 100;
private String name;
public abstract void fun();
public Animal(){
System.out.println(" ");
}
public void hello(){
System.out.println("hello,abstract!");
}
}
6.インタフェース
interface
* “ ”,
* : , !
* implements
* , !
* , new
public interface MyInterface {
// : ,
// :public static final ...,
String MAX_GREAD = "BOSS";
int MAX_SPEED = 100;
// :public abstract ...,
void test01();
int test02(int a, int b);
}
//
interface Interface03 extends Interface01, Interface02 ...{
}
7.多態性
1. OOP , 。 , , ,
2. 3 :
a. b. c.
public class Fruit {
private String color;
public void name(){
System.out.println("fruit");
}
}
public class Apple extends Fruit{ //
@Override
public void name(){ //
System.out.println("apple");
}
}
public class Test{
public static void main(String[] args) {
Fruit f = new Apple(); //
f.name();
}
}
// :apple
8.内部クラス
* 1. : , 。 ,
* 2.
* 3. ; ,
* 4.
public class TestInnerclass {
public static void main(String[] args) {
// , ,
World w = new World();
Human human = w.new Human();
human.hello();
// ,
Animal animal = new World.Animal();
animal.hello();
}
}
public class World{
private String hi = "hello";
public void say(String name){
System.out.println("hello,"+name);
}
//
class Human{
String name = "man";
void hello(){
//
System.out.println(hi);
say(name);
}
}
//
static class Animal{
String name = "animal";
void hello(){
//
World w = new World();
System.out.println(w.hi);
w.say(name);
}
}
}
9.匿名内部クラス
, ,
* 1.
* 2. , Thread Runnable
* 3. final
* 4. new ,
// Thread
public class Demo {
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
for (int i = 1; i <= 6; i++) {
System.out.print(i + " ");
}
}
};
t.start();
}
}
// Runnable
public class Demo {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
for (int i = 1; i <= 6; i++) {
System.out.print(i + " ");
}
}
};
Thread t = new Thread(r);
t.start();
}
}
// :1 2 3 4 5 6
10.異常処理
1. JDK , Throwable , ,
* Throwable
* / \
* Error Exception
* / / \
* Unchecked Exception Checked Exception Runtime Exception
* |
* Unchecked Exception
2. :
* 1. , , ,
* 2. try , , try catch, catch try
* 3.
3. Checked exception : ,try{...}catch(Exception e){e.printStackTrace();}finally{...}
* “ ”
*
* 1.try
* 2. try catch ,
* 3. , try
*
* 4.catch :
* e.printStackTrace();//
* e.getMessage();// ,
* e.toString();//
* Throwable
*
* 5.catch : , :
* , , ,
* :IOException FileNotFoundException , catch
* catch
*
* 6.finally , , !
* finally “ ”, : IO 、
*
* 7. :try,catch,return ——> finally
* 8. : finally return , try return
4. Checked exception : ,throws
* “ , ”
*
* 1. Checked exception , , throws
* 2. Checked exception, ,
*
* 3.@Override :
* 1. , ;
* 2. ;
* 3. ( )
5. Checked exception : ,throw
* , :
* 1.
* 2.
* 3.
File f = new File("C:/a.txt");
if(!f.exists){
try{
throw new FileNotFoundException("File can't be found!");
}catch(FileNotFoundException e){
e.printStackTrace();
}
}
6.
* 1.
* 2. Exception
* 3. , 2 : “ ” “ ”
public class MyException extends Exception{
//
public MyException(){
}
//
public MyException(String message){
super(message);
}
}