java 类和对象的理解

对象和类的定义


java 类和对象的理解_第1张图片



java 类和对象的理解_第2张图片




重载


public class Overloading {
 
	public int test(){
		System.out.println("test1");
		return 1;
	}
 
	public void test(int a){
		System.out.println("test2");
	}	
 
	//以下两个参数类型顺序不同
	public String test(int a,String s){
		System.out.println("test3");
		return "returntest3";
	}	
 
	public String test(String s,int a){
		System.out.println("test4");
		return "returntest4";
	}	
 
	public static void main(String[] args){
		Overloading o = new Overloading();
		System.out.println(o.test());
		o.test(1);
		System.out.println(o.test(1,"test3"));
		System.out.println(o.test("test4",1));
	}
}

重载 方法相同调用的参数类型、个数,顺序,修饰符不同打印的结果不同


打印的结果是

test1
1
test2
test3
returntest3
test4
returntest4


多态

public class test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		a am = new a();
		a an = new a();
		c cm = new c();
		d dm =  new d();
		
//		am.cc(new a(){
//			
//			@Override
//			public void cc(b bm) {
//				// TODO Auto-generated method stub
//				System.out.println("sdjghi");
//				
//			}	
//		});
		
//		am.cc(new b(){
//			
//			public void cc(b bm){
//					
//			}
//
//			@Override
//			public void ee() {
//				// TODO Auto-generated method stub
//				System.out.println("sdjghi");
//			}
//			
//		});
		
		cm.run(new b(){

			@Override
			public void cc(b bm) {
				// TODO Auto-generated method stub
				System.out.println("sdjghi");
			}

			@Override
			public void ee() {
				// TODO Auto-generated method stub
				System.out.println("sdjghi");
			}
			
			
		});
	
		cm.cc(cm);
		

	}

	private static b bm;
	public interface b{
		
		void cc(b bm);
		
		void ee();
	}
	
	
	
	static class a implements b {
		
//			a am = new a();
		@Override
		public void cc(b bm) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void ee() {
			// TODO Auto-generated method stub
			
		}	
	}
	
	static class c implements b{
		
		
		public void run( b bv ){
			
			bm =bv;
			bm.ee();
			bm.cc(bv);
		}
		public void play(){
			
			System.out.println("锻炼");
			
		}

		@Override
		public void cc(b bm) {
			// TODO Auto-generated method stub
			
			((c) bm).play();
		}

		@Override
		public void ee() {
			// TODO Auto-generated method stub
			
		}
	}
	
	static class d {
		
	}
}

打印的结果是 

sdjghi
sdjghi
锻炼


public class PolTest {

	public static class Wine {

		public void name1() {

			System.out.println("茅台");
		}

		public void name2(String name) {

			System.out.println("五粮液");
		}

		public void name2(String name, int a) {

			System.out.println("五粮液");
		}
	}

	public static class jnc extends Wine {

		public void name1() {

			System.out.println("特醇剑兰春");
		}

		public void name2(String name) {

			System.out.println("尊享剑兰春");
		}

		public void name2(String name, int a) {

			System.out.println("至尊剑兰春");
		}
		public void drink() {
			
			System.out.println("剑兰春好喝");
		}
		
	}
	public static class dq extends Wine {
		
		public void name1() {
			
			System.out.println("da");
		}
		
		public void name2(String name) {
			
			System.out.println("qu");
		}
		
		public void name2(String name, int a) {
			
			System.out.println("sdf");
		}
		public void drink() {
			
			System.out.println("hfg");
		}
		
	}
	
	
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Wine wine = new dq();
		wine.name2("有");
//		Wine wine = new Wine();
//		jnc jn= new jnc();
//		wine.name1();
//		jn.name1();
		
	}

}

打印结果是:qu

Java实现多态有三个必要条件:继承、重写、向上转型

所以基于继承实现的多态可以总结如下:对于引用子类的父类类型(继承),在处理该引用时(重写,向上转型),它适用于继承该父类的所有子类,子类对象的不同,对方法的实现也就不同,执行相同动作产生的行为也就不同


常量


public class A {

	public static void main(String[] args) {
		
		final int m =1;
		A a = new A();
		A b = new A();
		String c ="abc";
		String d ="abc";
		final int f =1;
		
		System.out.println(a);
		System.out.println("---------"+b);
		System.out.println(a==b);
		System.out.println(a.equals(b));
		System.out.println(c==d);
		System.out.println(c.equals(d));
		System.out.println(m==f);
		
		
		System.out.println(m);
	}
}

打印的结果是

A@2a139a55
---------A@15db9742
false
false
true
true
true
1


匿名类

类是有class关键字来修饰有名称并有“{}”,例如class A{} ,声明对象 A a = new A(); 匿名类没有名称所以称为匿名类如:

protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	findViewById(R.id.Button03).setOnClickListener(new OnClickListener(){
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(mainActivity.this, fristActivity.class);
			intent.putExtra("data", "mainActivity");
			startActivity(intent);			
		}		
	});
}

new OnClickListener(){
		@Override
		public void onClick(View v) {
					
		}		
	});  这个就是匿名类




原本应该是这样写的(不是匿名类的写法)


protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	findViewById(R.id.Button03).setOnClickListener(new A());
}


class A implements View.OnClickListener{
@Override
		public void onClick(View v) {
		
	Intent intent = new Intent(mainActivity.this, fristActivity.class);
			intent.putExtra("data", "mainActivity");
			startActivity(intent);	
			
		}

 
  

}





你可能感兴趣的:(Java,基础)