java面向對象的核心思想


 

java面向對象的特征之一:封裝

1、封裝性的使用

package edu.tongji.classdemo;
/*
封裝性
1.封裝的目的:保護某些屬性和方法不被外部所見
2.封裝的實現
為屬性和方法進行封裝通過關鍵愛你字private聲明
實現該屬性的set和get方法,為外部所訪問
*/
class Person{
	private int age;
	private String name;
	
	//以下可以快捷實現:
	//右鍵->source->generate getters and setters->選擇
	public int getAge(){
		return age;
	}
	public void setAge(int a){
		if(a>=0 && a<=150){
			this.age=age;
		}	
	}
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name=name;
	}
	
	public void tell(){
		System.out.println("年齡:"+getAge()+" "+"姓名:"+getName());
	}
}

public class ClassDemo01 {

	public static void main(String[] args) {
		Person per = new Person();
		/*per.age=30;
		per.name = "張三";*/
		per.setAge(-30);
		per.setName("張三");
		per.tell();
	}

}

2、匿名對象的使用

package edu.tongji.classdemo;

class Student{
	public void tell(){
		System.out.println("Hello Jikexueyuan");
	}
}

public class ClassDemo02 {

	public static void main(String[] args) {
		/*Student stu = new Student();
		stu.tell();*/
		//匿名對象:僅僅使用一次時很簡潔
		new Student().tell();

	}

}

3、構造對象的使用

package edu.tongji.classdemo;

class People{
	//構造方法
	/*
	 * 1.格式:
	 * 訪問修飾符  類名稱(){
	 * 程序語句
	 * }
	 * 2.注意點:
	 * (1)構造方法名稱必須與類一致
	 * (2)構造方法沒有返回值
	 * 3.構造方法主要為類中的屬性初始化
	 * 4.每個類在實例化后都會調用構造方法,如果沒有,程序在編譯時會創建一個無參的什么都不做的構造方法
	*/
	int age;
	String name;
	
	public People(int a,String n){
		age=a;
		name=n;		
		System.out.println("姓名:"+name+" "+"年齡:"+age);
	}
	
	//構造方法的重載
	public People(int a){
		age=a;	
		System.out.println("姓名:"+name);
	}
}

public class ClassDemo03 {

	public static void main(String[] args) {
		People per = new People(30,"張三");
	}

} 

引用的傳遞

1、引用傳遞

package com.tongji.ref;

class Ref1{
	int temp = 10;
}

public class RefDemo01 {

	public static void main(String[] args) {
		Ref1 r1 = new Ref1();
		r1.temp = 20;
		System.out.println(r1.temp);
		tell(r1);
		System.out.println(r1.temp);
	}
	//簡單的引用傳遞
	public static void tell(Ref1 r2){
		r2.temp = 30;
	}

}

 

package com.tongji.ref;

public class RefDemo02 {

	public static void main(String[] args) {
		String str1 = "hello";
		System.out.println(str1);
		tell(str1);
		System.out.println(str1);
	}
	public static void tell(String str2){
		str2="jike";
	}

}

 

package com.tongji.ref;

class Ref2{
	String temp = "hello";
}

public class RefDemo03 {

	public static void main(String[] args) {
		Ref2 r1 = new Ref2();
		r1.temp = "jike";
		System.out.println(r1.temp);
	    tell(r1);
	    System.out.println(r1.temp);
	}
	
	public static void tell(Ref2 r2){
		r2.temp = "xueyuan";
	}

}

2、this關鍵字

package edu.tongji.classdemo;

/*
 * this關鍵字
 * 1.表示類中的屬性和調用方法
 * 2.調用本類中的構造方法
 * 3.表示當前對象
*/
class People1{
	private String name;
	private int age;
	
	public People1(String name,int age){
		this();//2. 且只能放在首行
		this.name=name;//1.
		this.age=age;//1.
	}
	
	public People1(){
		System.out.println("無參數構造方法");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public void tell(){
		System.out.println("姓名:"+this.getName()+" 年齡:"+this.getAge());
	}
}

public class ThisDemo01 {

	public static void main(String[] args) {
		People1 p = new People1("張三",30);
		p.tell();
	}

}
package edu.tongji.classdemo;

class People2{
	public void tell(){
		System.out.println(this);//3.表示當前對象
	}
}

public class ThisDemo02 {

	public static void main(String[] args) {
		People2 p = new People2();
		System.out.println(p);
        p.tell();
	}

}

3、static關鍵字

package edu.tongji.classdemo;

/*
 * static關鍵字
 * 1.使用static聲明屬性:全局屬性
 * 2.使用static聲明方法:直接通過類名調用
 * 3.注意點:使用static方法時,只能訪問static聲明的屬性和方法,而非static聲明的屬性和方法是不能訪問的
 * 
*/

class Ren{
	String name;
	static String country="北京";
	
	public Ren(String name){
		this.name=name;
	}

	public void tell(){
		System.out.println("姓名:"+name+" 出生地:"+country);
	}
}

public class StaticDemo01 {	

	public static void main(String[] args) {
		Ren p1=new Ren("張三");
		Ren.country="上海";//靜態的屬性和方法通過類名直接調用
		p1.tell();
		Ren p2=new Ren("李四");
//		p2.country="上海";
		p2.tell();
		Ren p3=new Ren("王五");
//		p3.country="上海";
		p3.tell();

	}

}
package edu.tongji.classdemo;

public class StaticDemo02 {
	private static int i=10;
	public static void main(String[] args) {
		System.out.println(i);
		tell();//3.使用static方法時,只能訪問static聲明的屬性和方法,而非static聲明的屬性和方法是不能訪問的
	}
	public static void tell(){
		
	}

}

  

  

 

 

  

  

繼承 

1、繼承的實現

package edu.tongji.extendsdemo;

/*
 * 繼承的實現
 * 1、概念:擴展父類的功能
 * 2.java中使用extends關鍵字完成繼承:
 * class 子類 extends 父類{}
*/
class Person{
	private int age;
	private String name;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	/*public void tell(){
		System.out.println("姓名:"+getName()+" 年齡:"+getAge());
	}*/
}

class Student extends Person{
	private int score;

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}
	public void say(){
		System.out.println("成績:"+getScore()+" 姓名:"+getName()+" 年齡:"+getAge());
	}
}

public class ExtendsDemo01 {

	public static void main(String[] args) {
		Student s=new Student();
		s.setAge(20);
		s.setName("zhangsan");
		s.setScore(100);
	//	s.tell();
		s.say();
	}

}

2、繼承的限制

package edu.tongji.extendsdemo;
/*繼承的限制
 * 1.在java中只允許單繼承
 * 2.子類不能直接訪問父類的私有成員,需要實現其get和set方法進行訪問
*/
class People{
	private int age;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}



class Worker extends People{
	public void tell(){
		System.out.println(getAge());
	}
}

/*class PetWorker extends Worker{
	public void tell(){
		System.out.println();
	}
}*/

public class ExtendsDemo02 {

	public static void main(String[] args) {
		Worker w = new Worker();
		w.setAge(100);
		w.tell();
	}

}

3、子類對象的實例化

package edu.tongji.extendsdemo;
/*子類對象的實例化
 * 1.在子類對象實例化之前,必須先調用父類中的構造方法,之后調用子類構造方法
*/
class Father{
	private int age;
	private String name;
	
	public Father(){
		System.out.println("父類的構造方法");
	}
}

class Son extends Father{
	public Son(){
		//此處實際上是省略了super方法,編譯時會自動加上
		System.out.println("子類的構造方法");
	}
}

public class ExtendsDemo03 {

	public static void main(String[] args) {
		Son s = new Son();
	}

}

4、方法的重寫與super關鍵字

package edu.tongji.extendsdemo;

/*方法的重寫
 * 1、在繼承中,也存在着重寫的概念,其實就是子類定義了和父類同名的方法
 * 2、定義:
 * 方法名稱相同,返回值類型相同,參數也同
 * 3、重寫的限制:
 * 被子類重寫的方法不能擁有比父類方法更加嚴格的訪問權限
 * 4、訪問權限:
 * private(同一類內訪問)<default(同一包內訪問)<public(同一工程內訪問)
*/
class A{
	public void tell(){
		System.out.println("我是tell方法");
	}
	
	void print(){
		
	}
}

class B extends A{
	public void tell(){
		super.tell();  //1.super關鍵字:強行調用父類的方法的執行
		//補充:super不一定在重寫中使用,也可以表示那些方法是從父類中繼承而來,見Demo03
		System.out.println("我重寫了tell方法");
	}
	
	void print(){
		
	}
}

public class ExtendsDemo04 {

	public static void main(String[] args) {
		B b = new B();
		b.tell();
	}

}

重寫與重載的區別:

   

Java面向對象-抽象類與接口

1.Java final關鍵字的使用

package edu.tongji.fni;

/*final關鍵字
 * 1、final關鍵字在java中被稱為完結器,表示最終的意思
 * 2、final能聲明類、方法、屬性:
 * 使用final聲明的類不能被繼承
 * 使用final聲明的方法不能被重寫
 * 使用final聲明的變量變成常量,常量是不可以被修改的
*/

class People{  //前面加了final下面的類會報錯
	public void tell(){
		
	}
}

class Student extends People{
	public void tell(){
		
	}
}

public class FinalDemo01 {

	public static void main(String[] args) {
	    String name="jikexueyuan";//加final后下一句就會報錯
		name="www.jikexueyuan";
		
	}

}

2.java抽象類

package edu.tongji.fni;

/*抽象類
 * 1、抽象類概念:
 * 包含一個抽象方法的類就是抽象類
 * 2、抽象方法:聲明而未被實現的方法,抽象方法必須使用abstract關鍵字聲明
 * 3、抽象類被子類繼承,子類(如果不是抽象類)必須重寫類中的所有抽象方法
 * 4、定義格式:
 * abstract class className{
 * 屬性
 * 方法
 * 抽象方法
 * }
 * 5、抽象類不能直接實例化
*/
abstract class Abs{
	private int age;
	public void tell(){
		
	}
	//抽象方法
	public abstract void say();
	public abstract void print();
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}	
}

class AbsDemo extends Abs{
	public void say(){   //3.
		System.out.println(this.getAge());
	}
	public void print(){  //3.
		
	}
}

public class AbsDemo01 {

	public static void main(String[] args) {
		//Abs a=new Abs(); //5、抽象類不能直接實例化
		AbsDemo a=new AbsDemo();
		a.setAge(20);
		a.say();
		a.print();		
	}

}

3、java接口的實現

package edu.tongji.fni;

/*接口
 * 1、接口是java中最重要的概念,接口可以理解為一種特殊的類,里面全部是由全局常量和公共的抽象方法所組成
 * 2、接口的格式:
 * interface interfaceName{
 * 全局變量
 * 抽象方法
 * }
 * 3、接口的實現也必須通過子類,使用關鍵字implements,而且接口是可以多實現的
 * 4、一個子類可以同時繼承抽象類和實現接口
 * 5、一個接口不能繼承一個抽象類,但卻可以通過extends關鍵字同時繼承多個接口,實現接口的多繼承
*/
interface Inter1{
	public static final int AGE=100;//公共的全局常量(常量要大寫)
	public abstract void tell();//公共的抽象方法
}

interface Inter2{
	public abstract void say();
}

abstract class Abs1{
	public abstract void print();
}

class A extends Abs1 implements Inter1,Inter2{ //3、4、
	//需要重寫接口的方法
	@Override
	public void tell(){
		
	}
	public void say(){
		
	}
	public void print(){
		
	}
}

interface Inter3 extends Inter1,Inter2{  //5、   彌補java中單繼承的不足
	
}

public class InterDemo01 {

	public static void main(String[] args) {
		//Inter i=new Inter();//接口不能直接被使用
		A a=new A();
		a.tell();
		System.out.println(Inter1.AGE);
		a.say();
	}

}

接口和抽象類的相同點及不同點  

java面向對象多態性

1、多態性

package edu.tongji.pol;

/*多態性
 * 1、多態性的體現:
 * 方法的重載和重寫
 * 對象的多態性
 * 2、對象的多態性:
 * 向上轉型:程序會自動完成
 *         父類 父類對象=子類實例
 * 向下轉型:強制類型轉換
 *         子類 子類對象=(子類)父類實例
*/
class A{
	public void tell1(){
		System.out.println("A--tell1");
	}
	public void tell2(){
		System.out.println("A--tell2");
	}
}

class B extends A{
	public void tell1(){
		System.out.println("B--tell1");
	}
	public void tell3(){
		System.out.println("B--tell3");
	}
}

public class PolDemo01 {

	public static void main(String[] args) {
//		//向上轉型
//		B b=new B();
//		A a=b;  //父類 父類對象=子類實例
//		a.tell1();//tell1重寫的
//		a.tell2();
		
		//向下轉型
		A a=new B();  //一步實現向上轉型
		B b=(B)a;  //子類 子類對象=(子類)父類實例
		b.tell1();
		b.tell2();
		b.tell3();
	}

}

2.多態性的利用

package edu.tongji.pol;

class A1{
	public void tell1(){
		System.out.println("A--tell1");
	}
}

class B1 extends A1{
	public void tell2(){
		System.out.println("B--tell2");
	}
}

class C1 extends A1{
	public void tell3(){
		System.out.println("C--tell3");
	}
}

class D1 extends A1{
	
}

public class PolDemo02 {

	public static void main(String[] args) {
		say(new B1());
		say(new C1());
		say(new D1());//通過匿名對象的方式
	}
	/*當類很多時這種方式需要創建很多種方法,太麻煩,不如利用對象的多態性來調用簡潔
	public static void say(B1 b){
		b.tell1();
	}
	
	public static void say(C1 c){
		c.tell1();
	}
	*/
	public static void say(A1 a){
		a.tell1();
	}

}

3.instanceof關鍵字

package edu.tongji.pol;

/*多態性
 * 1、多態性的體現:
 * 方法的重載和重寫
 * 對象的多態性
 * 2、對象的多態性:
 * 向上轉型:程序會自動完成
 *         父類 父類對象=子類實例
 * 向下轉型:強制類型轉換
 *         子類 子類對象=(子類)父類實例
*/
class A{
	public void tell1(){
		System.out.println("A--tell1");
	}
	public void tell2(){
		System.out.println("A--tell2");
	}
}

class B extends A{
	public void tell1(){
		System.out.println("B--tell1");
	}
	public void tell3(){
		System.out.println("B--tell3");
	}
}

public class PolDemo01 {

	public static void main(String[] args) {
		A a=new A();
		System.out.println(a instanceof A);
		System.out.println(a instanceof B);
		
		A a1=new B();  //發生向上轉型后
		System.out.println(a1 instanceof A);
		System.out.println(a1 instanceof B);
	}

}

運行結果:

true
false
true
true

4、抽象類的應用

package edu.tongji.fni;
//抽象類的應用


abstract class Person{
	private int age;
	private String name;
	public Person (int age,String name){
		this.age=age;
		this.name=name;
	}
	public int getAge(){
		return age;
	}
	public void setAge(int age){
		this.age=age;
	}
	public String getName(){
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public abstract void want();	
}

class Xuesheng extends Person{
	private int score;
	public int getScore() {
		return score;
	}
	public Xuesheng(int age,String name,int score){
		super(age, name);
		this.score=score;
	}
	public void want(){
		System.out.println("姓名:"+getName()+" 年齡:"+getAge()+" 成績:"+getScore());
	}
}

class Worker extends Person{
	private int money;
	
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	
	public Worker(int age,String name,int money){
		super(age, name);
		this.money=money;
	}
	public void want(){
		System.out.println("姓名:"+getName()+" 年齡:"+getAge()+" 工資:"+getMoney());
	}
}

public class AbsDemo02 {

	public static void main(String[] args) {
		Xuesheng s=new Xuesheng(10,"小明",100);
		s.want();
		Worker w = new Worker(35, "大明",1000);
		w.want();
	}

}

執行結果:

姓名:小明 年齡:10 成績:100
姓名:大明 年齡:35 工資:1000

5、接口的使用

package edu.tongji.fni;

interface USB{
	void start();
	void stop();
}

class Computer{
	public static void work(USB u){
		u.start();
		System.out.println("工作中");
		u.stop();
	}
}

class USBDisk implements USB{   
	public void start(){
		System.out.println("U盤開始工作");
	}
	public void stop(){
		System.out.println("U盤停止工作");
	}
}

class Printer implements USB{
	@Override
	public void start() {
		System.out.println("打印機工作");		
	}
	@Override
	public void stop() {
		System.out.println("打印機停止工作");
	}
}

public class InterDemo02 {

	public static void main(String[] args) {
		Computer.work(new USBDisk());//由於只使用一次,采用匿名對象的方式調用
		Computer.work(new Printer());
	}

}

運行結果:

U盤開始工作
工作中
U盤停止工作
打印機工作
工作中
打印機停止工作

Java面向對象之泛型  

 1、認識泛型

package edu.tongji.Generic;

/*認識泛型(Generic)
 * 1、在JDK1.5之后出現的新功能
 * 2、泛型可以解決數據類型的安全性問題,它主要的原理是在類聲明的時候通過一個標識表示類中某個屬性的類型
 * 或者是某個方法的返回值及參數類型
 * 3、格式:
 * 訪問權限  class 類名稱<泛型,泛型...>{
 * 屬性
 * 方法
 * }
 * 4、對象的創建
 * 類名稱<具體類型> 對象名稱=new 類名稱<具體類型>();
*/

class Point<T>{
	private T x;
	private T y;
	public T getX() {
		return x;
	}
	public void setX(T x) {
		this.x = x;
	}
	public T getY() {
		return y;
	}
	public void setY(T y) {
		this.y = y;
	}
	
}

public class GenericDemo01 {

	public static void main(String[] args) {
		Point<String> p=new Point<String>();
		p.setX("精度為:109");
		p.setY("緯度為:100");
		System.out.println(p.getX()+" "+p.getY());
	}

}

執行結果:

精度為:109 緯度為:100

2、構造方法中使用泛型

package edu.tongji.Generic;

//構造方法中使用泛型:
//	構造方法可以為類中的屬性初始化,那么如果類中的屬性通過泛型指定,而又需要通過構造方法設置屬性內容的時候,
//那么構造方法的定義與之前並無不同,不需要像聲明類那樣指定泛型


class Attention<T>{
	private T value;
	public Attention(T value){
		this.value = value;
	}
	
	public T getValue(){
		return value;
	}
	public void setValue(T value) {
		this.value = value;
	}
}

public class GenericDemo02 {

	public static void main(String[] args) {
		Attention<String> c=new Attention<String>("構造方法中使用泛型");
		System.out.println(c.getValue());

	}

}

注意:一開始,我將類名命名為Con然后eclipse中編譯出現該問題:A class file was not written. The project may be inconsistent, if so try refreshing this project and building it,原來類名con是操作系統保留的一個設備名字,不可以使用。

其他的比如 CON, PRN, AUX, CLOCK$, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9都不可以.

3、指定多個泛型

package edu.tongji.Generic;

class Gen<K,T>{
	private T take;
	private K key;
	public T getTake() {
		return take;
	}
	public void setTake(T take) {
		this.take = take;
	}
	public K getKey() {
		return key;
	}
	public void setKey(K key) {
		this.key = key;
	}
	
}

public class GenericDemo03 {

	public static void main(String[] args) {
		Gen<String,Integer>g = new Gen<String,Integer>();
		g.setTake(10);
		g.setKey("key");
		System.out.println(g.getTake()+" "+g.getKey());
	}

}

4、通配符

package edu.tongji.Generic;

class Info<T>{
	private T key;

	public T getKey() {
		return key;
	}

	public void setKey(T key) {
		this.key = key;
	}
	public String toString(){
		return this.getKey().toString();
	}
}

public class GenericDemo04 {

	public static void main(String[] args) {
		Info<String> i = new Info<String>();
		i.setKey("nulifendou");
		tell(i);
	}
	
	/*通配符:?
	 * 作用:不論什么類型都可以表示
	*/
	public static void tell(Info<?> i){
		System.out.println(i);
	}

}

5、泛型接口

package edu.tongji.Generic;

interface GenInter<T>{
	public void say();
}

class Gin<T> implements GenInter<T>{
	private String info;
	public Gin(String info){
		this.info=info;
	}
	public String getInfo(){
		return info;
	}
	public void setInfo(String info){
		this.info=info;
	}
	public void say(){
	   
	}
}

public class GenericDemo05 {

	public static void main(String[] args) {
		Gin<String> g=new Gin<String>("nulifendou");
		System.out.println(g.getInfo());

	}

}

6、泛型方法

package edu.tongji.Generic;

/*泛型方法
 * 1、泛型方法中可以定義泛型參數,此時,參數的類型就是傳入的數據類型
 * 2、格式:
 * 訪問權限  <泛型標識> 泛型標識 方法名稱([泛型標識 參數標識])
*/
class Gener{
	public <T>T tell(T t){
		return t;//返回參數
	}
}

public class GenericDemo06 {

	public static void main(String[] args) {
		Gener g=new Gener();
		String str=g.tell("qingnengbuzuo");
		System.out.println(str);
		int i=g.tell(10);
		System.out.println(i);
	}

}

7、泛型數組

package edu.tongji.Generic;

//1、在使用泛型方法時,也可以傳遞或返回一個泛型數組

public class GenericDemo07 {

	public static void main(String[] args) {
		Integer arr[]={1,2,3,4};
		tell(arr);
	}
	
	public static <T> void tell(T arr[]){
		for(int i=0;i<arr.length;i++){
			System.out.println(arr[i]);
		}
	}

}

執行結果:

1
2
3
4

  

  

  

  

  

  

 

  

  

  

  

  

  

  

  

 

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM