package cn.itcast.liukong;
public class TestThis {
public void jump(){
System.out.println("it's jumping now!");
}
public void run(){
jump();
this.jump();/*/d等效于里面的代码。this 关键字最大作用是让类中一个方法,
访问该类的另一个方法或属性。*/
TestThis dog = new TestThis();
dog.jump();
System.out.println("it's running now!!");
}
}
package cn.itcast.liukong;
public class Testdog {
public static void main(String[]args){
TestThis Tdog=new TestThis();
Tdog.run();
}
}
package cn.itcast.liukong;
public class TestStaticthis {
public static void main(String[]args){
new TestStaticthis().run();//从新创建一个对象来调用非静态方法。静态方法无法访问非静态方法
//run();
/*Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method run() from the type TestStaticthis
静态方法无法访问非静态方法。this.run()
at cn.itcast.liukong.TestStaticthis.main(TestStaticthis.java:5)*/
}
public void run(){
System.out.println("run!!");
}
}
package cn.itcast.liukong;
public class TestRefenceTransfer {
public static void swap(DataWrap dw){
int tmp=dw.a;
dw.a=dw.b;
dw.b=tmp;
System.out.println("swap方法中的a的属性值是:"+dw.a+";b的属性值是:"+dw.b);
dw=null;
}
public static void main(String[]args){
DataWrap dw= new DataWrap();
dw.a=6;
dw.b=9;
swap(dw);
System.out.println("交换结束后,a属性是"+dw.a+";b属性的值是"+dw.b);
}
}
package cn.itcast.liukong;
public class TestPrimitiveTransfer {
public static void swap(int a ,int b){
int tmp;
tmp=a;
a=b;
b=tmp;
System.out.println("a="+a+ ";b="+b);
}
public static void main(String[]args){
int a=3;
int b=90;
swap(a,b);
System.out.println("a="+a+";b="+b);
}
}
package cn.itcast.liukong;
public class Resursive {
public static int intf(int n){//此番方法需要有返回值为整形,故 非void 而int
if(n==0)
return 1;
else if (n==1)
return 4;
else
return 2*intf(n-1)+intf(n-2);//递归;;;
}
public static void main(String[]args){
System.out.println(intf(3));
}
}
package cn.itcast.liukong;
public class Person {
public String name;
public static int EyeNum;
}
package cn.itcast.liukong;
public class TestPerson {
public static void main(String[]args){
System.out.println("Person的EyeNum类的属性值是:"+Person.EyeNum);
Person p=new Person();
System.out.println("p变量的属性值是:p.EyeNum:"+p.EyeNum+";p.name:"+p.name);
p.name="悟空!";
p.EyeNum=2;
System.out.println("p.name: "+p.name+"p.EyeNum: "+p.EyeNum);
System.out.println("Person.EyeNum: "+Person.EyeNum);
}
/*输出结果:
* Person的EyeNum类的属性值是:0
* p变量的属性值是:p.EyeNum:0;p.name:null
* p.name: 悟空!p.EyeNum: 2
* Person.EyeNum: 2
*/
}
public class OverLoad {
public void test(){
System.out.println("无参数方法");
}
public static int test(int a,int b){
int temp;
temp=a;
a=b;
b=temp;
System.out.println("有参数");
return a;
}
/*
* |实例属性(非static修饰)
* |成员变量
* | |类属性(以static修饰)
* 变量
* | |形参(方法签名中定义的变量)
* | |方法局部变量(在方法内定义)
* |局部变量
* | |代码块局部变量(在代码块内部定义)
* */
public static void main(String[]args){
OverLoad ol=new OverLoad();
ol.test();
System.out.println(test(2,20));
}
}
package cn.itcast.liukong;
public class beachifwhile {
public static void main(String[] args)
{
int age=10;
if(age>20&age<=40)
System.out.println("青年人");
else if (age<60&age>=40)
System.out.println("中年人");
else if(age>=60)
System.out.println("老年人");
else
System.out.println("少儿");
}
}
本文出自 “醉痴东风” 博客,谢绝转载!