public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("大家好!");//向控制台输出语句
System.out.println("Nice to meet you");//向控制台输出语句 Student stu = new Student();//类确定对象
stu.speak("We are students");//stu对象向控制台输出语句}
}
class Student {
public void speak (String s){//创建新的类
System.out.println(s);
}
}
class Student {
public void speak (String s){//创建新的类
System.out.println(s);
例子2
public class People {//定义一个公共访问权限的类People
/**
* @param args
int height;//定义浮点型数据height
String ear;//定义浮点型数据heightear
void speak(String s){
//定义speak()方法,该方法的功能是输出参数的值
System.out.println(s);
}
}
class A {
public static void main(String[] args) {
// TODO Auto-generated method stub
People zhubajie;//声明一个People类的对象zhubajie
zhubajie = new People();
//为对象zhubajie分配储存空间(通过关键字new和构造方法)
zhubajie.height =170;
//给对象zhubajie的height变量赋值1.7
zhubajie.ear = "两只大耳朵";
System.out.println("身高:"+zhubajie .height);
System.out.println(zhubajie.ear);
//调用对象zhubajie的speak()方法
zhubajie.speak("师傅,咱们别去西天了,改去月宫吧");
}
}。