Chinaunix首页 | 论坛 | 博客
  • 博客访问: 242458
  • 博文数量: 91
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-12 09:38
文章分类

全部博文(91)

文章存档

2017年(1)

2011年(1)

2008年(15)

2007年(74)

我的朋友

分类: Java

2007-09-16 14:22:57

the first program hello
java程序的运行方式
public class hello{
public static void main(String args[])
 {
 System.out.println("welcome to java world");
}
}
保存的名字为 hello.java
运行:javac hello.java
     java name
 保存的名字必须与主类名相同。如果有多个类时,只能有一个public类,一个main。
 如果在程序的开头加上了包名(文件夹),形如:
 package javas
 保存的名字为javas/hello.java
 运行时,如果在javas的上一级目录,必须加上路径,如:
 javac javas/hello.java
 java javas/hello
 也只能这样运行,不能像如下这样
 cd javas
 javac hello.java
 java hello
 
 假如你定义了二个类(continue1, break_1),其中的一个类(continue1)想调用另一个类(break_1)的变量或函数,而这二个类都不是主类,唯一的办法就是在调用类的构造函数中定义另一类的对象。
 如:
 
class continue1
//the variable name can same in different class, such as follow variable a
{int a=0;
//break_1 x1=new break_1();这种调用是错误的
//x1.ac();
//this is a structor function
 continue1()
{System.out.println(a);
break_1 x1=new break_1();
x1.ac();
}
void con()
{
 for(int i=0;i<=20;i++)
{if(i%3==0)
 continue;
 else
 System.out.print(i+"");
}
}
}

class break_1
{
void ac()
{
int n=0;
boolean a=true;
 while(a)
{if(n==15) break;
 n++;
}
System.out.println(n);
}
}

使用下面的函数,可以方便的从键盘输入数据:
使用方法举例:
void input1()
{
 try{
        char ch;
        do
        {
         System.out.println("please input characters and enter,press # terminate");
        ch=(char)System.in.read();
        System.in.skip(2);//skip the enter and \b
        System.out.println(ch+"\b"+"\'s ASCII value is"+(int)ch);
        }while(ch!='#');
    }catch(Exception e){}
}
}

在java中的方法(函数)可以先调用,后定义,且不需声明。如:
class cc
{
 double dc=3.5+4.8;
 int i=10;
//it don't prior declare before invoking fuction, which defined below
int j=go(i);
int go(int yyy)
{
return yyy*yyy;
}
}

在调用有参构造器时,是通过建立对象时调用的。如:
class aa
{
 aa(String str)
{
 System.out.println(str);
}
//when define a structor whith parameter, then must define a have not parameter
structor
aa()
{}
}
class bb
{
aa x1=new aa("hello");
}

java里的修饰符有四种
public, private, protected, default(在同一个包(文件夹)下所有的类都可以相互访问)

java类中的变量和函数的另一种调用(前提是该变量或函数为static,否则就会出错)
 如:class a44
{static int counter=0;
static int getcounter()
{
return counter;
}
                                                                                
 public a44()
{
counter++;
}
}

class cc
{
cc()
{

//this is a new invoking method, class name invoking
System.out.println("a44.counter="+a44.counter);
System.out.println("a44.getcouter()="+a44.getcounter());
//System.out.println(a44.counter);
}
}
类的调用与先后顺序无关

父类的句柄不能调用子类的方法和变量,而子类可以调用父类的,如父类和子类有相同的方法名时,则称为对象替换,如下:
chinaman man1=new chinaman();
chinaman man2=new wuhan();
man1.say();
man2.say();
//man2.say1();
}
}
class chinaman
{
void say()
{
System.out.println("I is a chinaman");
}
}
class wuhan extends chinaman
{
void say()
{
System.out.println("I is a wuhan man");
System.out.println("I is a wuhan man");
}
void say1()
{
System.out.println("I love wuhan");
}
}
oop_2.java
在一个类中,如有多个构造器时,构造器之间的调用用this比较方便,(this指向当前的对象)如下:
public class oop_3
{
public static void main(String args[])
{
new a2("hello");
                                                                                
}
}
class a2
{
String ch="how are you";
a2()
{System.out.println(ch);}
a2(String str)
{
//invoking between structors in a same class
this();
this.ch=str;
System.out.println(ch);
}
}
oop_3.java
在构造器中不要调用动态的方法,否则会得到意想不到的结果,调用时最好是final, static, private,修饰的函数
看看如下方法运行的结果,我运行的结果为111,而不是999
class add
{protected int m=880;
 add()
{m=ad(8);}
int ad(int i)
{
return m+i;
}
}
class add1 extends add
{
protected int ss=111;
add1()
{
m+=ss;
}
int ad(int i)
{
return i*ss;
}
}
class a3
{
a3()
{add1 xx=new add1();
System.out.println(xx.m);}
}
//接口的例子oop_3.java
class xx{
xx()
{
student stu=new student();
stu.rest(new football());
stu.rest(new computer());
                                                                                
stu.rest(new ball(),"pring peng");


}
}
//the follow is a interface, interface'
s state equal to class
interface playable//定义了一个接口
{
public String play();
}
class ball
{
public String play(String str)
{
return "play " +str+"ball";
                                                                                                                                      
}
}
class football extends ball implements playable//接口的实现
{
public String play()
{
return "play football, extra station team";
}
}
class computer implements playable
{
public String play()
{
return "play computer game, don't loss sense";
}
}
class student
{
//noting interface have not object, following have
//接口无自己的对象,在下面的接口对象的句柄只起着占位符的作用,无实际意义。
void rest(playable ating)
 {
void rest(playable ating)
 {
 System.out.println(ating.play());
}
void rest(ball abal, String str)
{
System.out.println(abal.play(str));
}
}
多态就是函数的重载,如:
fun(int i, int j)
{
System.out.println(i+j);
}
fun(double i, int j)
{
System.out.println(i*j);
}
只是调用时参数不同而已
oop_3.java
内部类(就是在类中再定义类)
如:
class student
{
student()
{
new cc();
}
}
class cc
{
int xy=9;
class dd
{
void printx()
{
 System.out.println("this is a inner class");
 System.out.println("xy= "+xy);
}
}
cc()
{
dd d2=new dd();
d2.printx();
}
}




阅读(745) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~