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