java零基础学习笔记(代码+注解+练习)_面向对象OOP

package oo.day01;
//学生类
public class Student {
	//成员变量
	String name;
	int age;
	String address;
	
	//方法
	void study() {
		System.out.println(name+"在学习...");
	}
	void sayHi() {
		System.out.println("大家好,我叫"+name+",今年"+age+"岁了,家住"+address);
	}
}

package oo.day01;
//学生类的测试类
public class StudentTest {
	public static void main(String[] args) {
		//创建学生对象
		Student zs = new Student();
		//给成员变量赋值
		zs.name = "zhangsan";
		zs.age = 25;
		zs.address = "河北廊坊";
		//调用方法
		zs.study();
		zs.sayHi();
		
		Student ls = new Student();
		ls.name = "lisi";
		ls.age = 26;
		ls.address = "黑龙江佳木斯";
		ls.study();
		ls.sayHi();
		
		//1)创建了一个学生对象
		//2)给所有成员变量赋默认值
		Student ww = new Student();
		ww.study();
		ww.sayHi();
	}
}

package oo.day02;
//构造方法和this的演示
public class ConsDemo {
	public static void main(String[] args) {
		Student zs = new Student();
		Student ls = new Student("lisi",25,"LF");
		zs.sayHi();
		ls.sayHi();
	}
}

package oo.day02;
//重载的演示
public class OverloadDemo {
	public static void main(String[] args) {
		Aoo o = new Aoo();
		o.say();
		o.say("zhangsan");
		o.say(25);
		o.say("zhangsan", 25);
		o.say(25, "zhangsan");
	}
}
class Aoo{
	void say() {}
	void say(String name) {}
	void say(int age) {}
	void say(String name,int age) {}
	void say(int age,String name) {}
	//int say() {return 1;} //编译错误,重载与返回值类型无关
	//void say(String address) {} //编译错误,重载与参数名称无关
}

package oo.day02;
//学生类
public class Student {
	String name;
	int age;
	String address;
	
	//构造方法
	Student(){
		this("无名氏",1,"未知"); //调用构造方法
	}
	Student(String name,int age,String address){
		this.name = name;
		this.age = age;
		this.address = address;
	}
	
	void study() {
		System.out.println(name+"在学习...");
	}
	void sayHi() {
		System.out.println("大家好,我叫"+name+",今年"+age+"岁了,家住"+address);
	}
}



package oo.day03;
//引用类型数组的演示
public class ArrayDemo {
	public static void main(String[] args) {
		Student[] stus = new Student[3]; //创建Student数组对象
		stus[0] = new Student("zs",25,"LF"); //创建Student对象
		stus[1] = new Student("ls",26,"JMS");
		stus[2] = new Student("ww",28,"SD");
		System.out.println(stus[0].name); //输出第1个学生的名字
		stus[1].age = 24; //修改第2个学生的年龄为24
		stus[2].sayHi(); //第3个学生跟大家问好
		for(int i=0;i<stus.length;i++) { //遍历所有学生
			System.out.println(stus[i].name); //输出每个学生的名字
			stus[i].sayHi(); //每个学生跟大家问好
		}
		
		Student[] ss = new Student[]{ //了解
				new Student("zs",25,"LF"),
				new Student("ls",26,"JMS"),
				new Student("ww",28,"SD")
	    };
		
	}
}
package oo.day03;
//学生类
public class Student {
	String name;
	int age;
	String address;
	
	//构造方法
	Student(){
		this("无名氏",1,"未知"); //调用构造方法
	}
	Student(String name,int age,String address){
		this.name = name;
		this.age = age;
		this.address = address;
	}
	
	void study() {
		System.out.println(name+"在学习...");
	}
	void sayHi() {
		System.out.println("大家好,我叫"+name+",今年"+age+"岁了,家住"+address);
	}
}



package oo.day03;
//super的演示
public class SuperDemo {
	public static void main(String[] args) {
		Boo o = new Boo();
	}
}


class Coo{
	Coo(int a){
		
	}
}
class Doo extends Coo{
	Doo(){
		super(5);
	}
	/*
	//如下代码为默认的
	Doo(){
		super();
	}
	*/
}



class Aoo{
	Aoo(){
		System.out.println("超类构造");
	}
}
class Boo extends Aoo{
	Boo(){
		//super(); //默认的,调用超类的无参构造
		System.out.println("派生类构造");
	}
}
package oo.day04;
public class Test {
	public static void main(String[] args) {
		Student[] stus = new Student[3];
		stus[0] = new Student("aaa",25,"LF","111");
		stus[1] = new Student("bbb",24,"JMS","222");
		stus[2] = new Student("ccc",26,"SX","333");
		for(int i=0;i<stus.length;i++) {
			System.out.println(stus[i].name);
			stus[i].sayHi();
		}
		
		Teacher[] tes = new Teacher[3];
		tes[0] = new Teacher("ddd",35,"LF",5000);
		tes[1] = new Teacher("eee",44,"JMS",8000);
		tes[2] = new Teacher("fff",56,"SD",10000);
		for(int i=0;i<tes.length;i++) {
			System.out.println(tes[i].name);
			tes[i].sayHi();
		}
		
		Doctor[] docs = new Doctor[2];
		docs[0] = new Doctor("ggg",35,"LF","主治医师");
		docs[1] = new Doctor("hhh",44,"LF","副主任医师");
		for(int i=0;i<docs.length;i++) {
			System.out.println(stus[i].name);
			docs[i].sayHi();
		}
	}
}

class Person{
	String name;
	int age;
	String address;
	Person(String name,int age,String address){
		this.name = name;
		this.age = age;
		this.address = address;
	}
	void sayHi() {
		System.out.println("大家好,我叫"+name+",今年"+age+"岁了,家住"+address);
	}
}
class Student extends Person{
	String stuId; //学号
	Student(String name,int age,String address,String stuId){
		super(name,age,address);
		this.stuId = stuId;
	}
}
class Teacher extends Person{
	double salary; //工资
	Teacher(String name,int age,String address,double salary){
		super(name,age,address);
		this.salary = salary;
	}
}
class Doctor extends Person{
	String level; //职称
	Doctor(String name,int age,String address,String level){
		super(name,age,address);
		this.level = level;
	}
}


package oo.day04;
//重写的演示
public class OverrideDemo {
	public static void main(String[] args) {
		
	}
}



//超类大,派生类小
class Coo{
	void show() {}
	double test() {return 0.0;}
	Doo say() {return null;}
	Coo sayHi() {return  null;}
}
class Doo extends Coo{
	//int show() {return 5;} //编译错误,void时必须相同
	//int test() {return 0;} //编译错误,基本类型时必须相同
	//Coo say() {return null;} //编译错误,引用类型时必须小于或等于
	Doo sayHi() {return  null;} //正确,小于或等于
}


package oo.day04;
//重写与重载区别的演示
public class OverrideOverloadDemo {
	public static void main(String[] args) {
		Eoo eoo = new Eoo(); //想调show()方法必须创建Eoo对象
		Aoo o = new Boo();   //想调show()方法必须得传参
		eoo.show(o); //重载方法被调用时,看参数的类型
	}
}

class Eoo{
	void show(Aoo o) {
		System.out.println("超类型参数");
	}
	void show(Boo o) {
		System.out.println("派生类型参数");
	}
}


class Aoo{
	int a;
	void show() {
	}
}
class Boo extends Aoo{
	int b;
	void test() {
	}
}

package oo.day04;
public class Test {
	public static void main(String[] args) {
		Person[] ps = new Person[5];
		ps[0] = new Student("zhangsan",25,"LF","111"); //向上造型
		ps[1] = new Student("lisi",26,"JMS","222");
		ps[2] = new Teacher("wangwu",35,"SD",8000.0);
		ps[3] = new Teacher("zhaoliu",46,"SX",18000.0);
		ps[4] = new Doctor("sunqi",35,"LF","主任医师");
		for(int i=0;i<ps.length;i++) {
			System.out.println(ps[i].name);
			ps[i].sayHi();
		}
		
		//重写方法被调用时,看对象的类型----规定
		Student zs = new Student("zhangsan",25,"LF","111");
		Person s = new Student("zhangsan",25,"LF","111");
		zs.sayHi(); //调用Student类的sayHi()
		s.sayHi();  //调用Student类的sayHi()
		
	}
}

class Person{
	String name;
	int age;
	String address;
	Person(String name,int age,String address){
		this.name = name;
		this.age = age;
		this.address = address;
	}
	void sayHi() {
		System.out.println("大家好,我叫"+name+",今年"+age+"岁了,家住"+address);
	}
}
class Student extends Person{
	String stuId; //学号
	Student(String name,int age,String address,String stuId){
		super(name,age,address);
		this.stuId = stuId;
	}
	void sayHi() {
		System.out.println("大家好,我叫"+name+",今年"+age+"岁了,家住"+address+",学号为:"+stuId);
	}
}
class Teacher extends Person{
	double salary; //工资
	Teacher(String name,int age,String address,double salary){
		super(name,age,address);
		this.salary = salary;
	}
	void sayHi() {
		System.out.println("大家好,我叫"+name+",今年"+age+"岁了,家住"+address+",工资为:"+salary);
	}
}
class Doctor extends Person{
	String level; //职称
	Doctor(String name,int age,String address,String level){
		super(name,age,address);
		this.level = level;
	}
}

package oo.day04;
//向上造型的演示
public class UploadDemo {
	public static void main(String[] args) {
		Aoo1 o1 = new Aoo1();
		o1.a = 1;
		o1.show();
		//o1.b = 2; //编译错误,超类不能访问派生类的
		
		Boo1 o2 = new Boo1();
		o2.b = 1;
		o2.test();
		o2.a = 2;  //正确,派生类可以访问超类的
		o2.show();
		
		Aoo1 o3 = new Boo1(); //向上造型
		o3.a = 1;
		o3.show();
		//o3.b = 2;  //编译错误,能点出来什么,看引用o3的类型
		//o3.test(); //编译错误
	}
}
class Aoo1{
	int a;
	void show() {
	}
}
class Boo1 extends Aoo1{
	int b;
	void test() {
	}
}

package oo.day05;
//访问控制修饰符的演示
public class Aoo {
	public int a;    //任何类
	protected int b; //本类、派生类、同包类
	int c;           //本类、同包类
	private int d;   //本类
	
	void show() {
		a = 1;
		b = 2;
		c = 3;
		d = 4;
	}
}

class Boo{ //演示private
	void show() {
		Aoo o = new Aoo();
		o.a = 1;
		o.b = 2;
		o.c = 3;
		//o.d = 4; //编译错误
	}
}





package oo.day05.vis;
import oo.day05.Aoo;
public class Coo { //演示同包的
	void show() {
		Aoo o = new Aoo();
		o.a = 1;
		//o.b = 2; //编译错误
		//o.c = 3; //编译错误
		//o.d = 4; //编译错误
	}
}

class Doo extends Aoo{ //跨包继承,演示protected
	void show() {
		a = 1;
		b = 2;
		//c = 3; //编译错误
		//d = 4; //编译错误
	}
}

package oo.day05;
//final的演示
public class FinalDemo {
	public static void main(String[] args) {
	}
}


//演示final修饰类
final class Hoo{}
//class Ioo extends Hoo{} //编译错误,final的类不能被继承
class Joo{}
final class Koo extends Joo{} //正确,final的类不能当老爸,但能当儿子



//演示final修饰方法
class Foo{
	final void show() {}
	void test() {}
}
class Goo extends Foo{
	//void show() {} //编译错误,final的方法不能被重写
	void test() {}
}




//演示final修饰变量
class Eoo{
	final int a = 5;
	void test() {
		//a = 8; //编译错误,final的变量不能被改变
		final int b = 55;
		//b = 88; //编译错误,final的变量不能被改变
	}
}



package oo.day05;
//static的演示
public class StaticDemo {
	public static void main(String[] args) {
		Loo o1 = new Loo();
		o1.show();
		Loo o2 = new Loo();
		o2.show();
		Loo o3 = new Loo();
		o3.show();
		System.out.println(Loo.b); //常常通过类名点来访问
		
		Moo.test(); //常常通过类名点来访问
		
		Noo o4 = new Noo();
		Noo o5 = new Noo();
		Noo o6 = new Noo();
	}
}

class Noo{ //演示静态块
	static {
		System.out.println("静态块");
	}
	Noo(){
		System.out.println("构造方法");
	}
}





class Moo{ //演示静态方法
	int a;        //实例变量(由对象点来访问)
	static int b; //静态变量(由类名点来访问)
	
	void show() { //有隐式this
		System.out.println(this.a);
		System.out.println(Moo.b);
	}
	static void test() { //没有隐式this
		//静态方法中没有隐式的this传递
		//没有this就意味着没有对象
		//而实例变量a必须通过对象名来访问
		//所以如下语句发生编译错误
		//System.out.println(a); //编译错误
		System.out.println(Moo.b);
		
	}
}

class Loo{ //演示静态变量
	int a;
	static int b;
	Loo(){
		a++;
		b++;
	}
	void show() {
		System.out.println("a="+a+",b="+b);
	}
}



package oo.day06;
//static final常量的演示
public class StaticFinalDemo {
	public static void main(String[] args) {
		System.out.println(Aoo.PI); //常常通过类名点来访问
		//Aoo.PI = 3.1415926; //编译错误,常量不能被改变
		
		
		
		//1)加载Boo.class到方法区
		//2)静态变量num一并存储到方法区中
		//3)到方法区中获取num的值并输出
		System.out.println(Boo.num);
		
		//编译器在编译时会将常量直接替换为具体的值,效率高
		//相当于System.out.println(5);
		System.out.println(Boo.COUNT);
	}
}

class Boo{
	public static int num = 5;         //静态变量
	public static final int COUNT = 5; //常量
}


class Aoo{
	public static final double PI = 3.14159;
	//public static final int NUM; //编译错误,常量必须声明同时初始化
}



package oo.day07;
//成员内部类的演示
public class InnerClassDemo {
	public static void main(String[] args) {
		Mama m = new Mama();
		//Baby b = new Baby(); //编译错误,内部类对外不具备可见性
	}
}

class Mama{ //外部类
	private String name;
	void create() {
		Baby b = new Baby(); //正确
	}
	class Baby{ //内部类
		void showName() {
			System.out.println(name);
			System.out.println(Mama.this.name);
			//System.out.println(this.name); //编译错误,this表示当前Baby对象
		}
	}
}


package oo.day07;
//匿名内部类的演示
public class NstInnerClassDemo {
	public static void main(String[] args) {
		//1)创建了Aoo的一个派生类,但是没有名字
		//2)为该派生类创建了一个对象,名为o1
		//3)大括号中的为派生类的类体
		Aoo o1 = new Aoo() {
			
		};
		
		//1)创建了Aoo的一个派生类,但是没有名字
		//2)为该派生类创建了一个对象,名为o2
		//3)大括号中的为派生类的类体
		Aoo o2 = new Aoo() {
			
		};
		
		final int num = 5;
		//1)创建了Boo的一个派生类,但是没有名字
		//2)为该派生类创建了一个对象,名为o3
		//3)大括号中的为派生类的类体
		Boo o3 = new Boo() {
			void show() {
				System.out.println("showshow");
				System.out.println(num); //JDK1.7(含)以前要求该变量必须是final的
			}
		};
		o3.show();
		
	}
}

abstract class Boo{
	abstract void show();
}

abstract class Aoo{
}



package oo.day08;
//接口的演示
public class InterDemo {
	public static void main(String[] args) {
		//Inter3 o1 = new Inter3(); //编译错误,接口不能被实例化
		Inter3 o2 = new Coo(); //向上造型
		Inter2 o3 = new Coo(); //向上造型
	}
}
interface Inter2{ 
	void say();
}
interface Inter3 extends Inter2{
	void sayHello();
}
class Coo implements Inter3{
	public void sayHello() {}
	public void say() {}
}



interface Inter1{
	void show(); //接口中成员默认访问权限为public的
	void test();
}




abstract class Aoo{
	abstract void sayHi();
}

class Boo extends Aoo implements Inter1,Inter2{
	public void show() {} //类中成员默认访问权限为默认的
	public void test() {}
	public void say() {}
	void sayHi() {}
}




//演示接口的语法
interface Inter{
	public static final int NUM = 5;
	public abstract void show();
	double PI = 3.14159; //默认public static final
	void test(); //默认public abstract
	
	//int count; //编译错误,常量必须声明同时初始化
	//void sayHi() {} //编译错误,抽象方法不能有方法体
}


package oo.day09;
//多态的演示
public class MultiTypeDemo {
	public static void main(String[] args) {
		Aoo o = new Boo(); //向上造型
		Boo o1 = (Boo)o; //引用o所指向的对象就是Boo类型
		Inter o2 = (Inter)o; //引用o所指向的对象实现了Inter接口
		//Coo o3 = (Coo)o; //运行时发生ClassCastException类型转换异常
		if(o instanceof Coo) { //建议强转时都要做这样判断,此处为false
			Coo o4 = (Coo)o;
		}else {
			System.out.println("o不是Coo类型");
		}
	}
}
interface Inter{}
class Aoo{}
class Boo extends Aoo implements Inter{}
class Coo extends Aoo{}



package oo.day10;
import java.util.Arrays;
//补充内容
public class Test {
	public static void main(String[] args) {
		/*
		int[] arr = {34,6,16,3,89};
		//将arr数组转换为字符串并输出
		System.out.println(Arrays.toString(arr));
		*/
	
		Student zs = new Student();
		zs.setName("zhangsan");
		System.out.println(zs.getName());
		zs.setAge(25);
		int age = zs.getAge();
		System.out.println(age);
	}
}

class Student{
	private String name;
	private int age;
	
	public void setName(String name) { //赋值
		this.name = name;
	}
	public String getName() { //取值
		return name;
	}
	
	public void setAge(int age) { //赋值
		this.age = age;
	}
	public int getAge() { //取值
		return age;
	}	
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值