Example01
public class Example01 {
public static void main (String[] ags)
{
Person p1=new Person(); //创建第一个Person对象
Person p2=new Person(); //创建第二个Person对象
p1.age=18; //为age赋值
p1.speak(); //调用对象的方法
p2.speak();
}
}
class Person {
int age;
void speak(){
System.out.println("hello,I am "+age+" years old!");
}
}
Example02
public class Example02 {
public static void main (String[] ags)
{
Person p1=new Person();
p1.say();
p1=null;
p1.say();
}
}
class Person {
void say (){
System.out.println("I am people!");
}
}
Example03
public class Example03 {
public static void main (String[] ags)
{
Student stu=new Student();
stu.name="石钊宇";
stu.age=20;
stu.introduce();
}
}
class Student{
String name;
int age;
public void introduce(){
System.out.println("大家好,我叫"+name+"今年"+age+"岁了。");
}
}
Example04
public class Example04 {
public static void main (String[] ags)
{
Student stu=new Student();
stu.setAge(20);
stu.setName("石钊宇");
stu.introduce();
}
}
class Student{
private String name;
private int age;
public String getName(){
return name;
}
public void setName(String stuName){
name=stuName;
}
public int age(){
return age;
}
public void setAge(int stuAge){
if(stuAge<=0){
System.out.println("输入的年龄不合法!");
}
else{
age=stuAge;
}
}
public void introduce(){
System.out.println("大家好,我叫"+name+"今年"+age+"岁了。");
}
}
Example05
public class Example05 {
public static void main (String[] ags)
{
Person p=new Person();
}
}
class Person
{
public Person(){
System.out.println("无参的构造方法被调用.......");
}
}
Example06
public class Example06 {
public static void main (String[] ags)
{
Person p=new Person(20);
p.say();
}
}
class Person
{
int age;
public Person(int a){
age=a;
}
public void say (){
System.out.println("I am "+age+" years old!");
}
}
Example07
public class Example07 {
public static void main (String[] ags)
{
Person p1=new Person("Stone",20);
Person p2=new Person("STONE");
p1.say();
p2.say();
}
}
class Person{
String name;
int age;
public Person (String con_name,int con_age){
name=con_name;
age=con_age;
}
public Person (String con_name){
name=con_name;
}
public void say(){
System.out.println("I am"+name+", I am "+age+" years old!");
}
}
Example08
public class Example08 {
public static void main (String[] ags)
{
Person p=new Person();
}
}
/*
报错的原因是调用new Person()创建Person类的实例对象时,需要调用无参的构造方法
而我们并没有定义无参的构造方法,只是定义了一个有参的构造方法,系统将不再自动生成无参的构造方法。
*/
Example09
public class Example09 {
public static void main (String[] ags)
{
Person p=new Person();
}
}
class Person
{
public Person(){
//private
System.out.println("调用无参方法");
}
}
Example10
public class Example10 {
public static void main (String[] ags)
{
Person p=new Person("itcast");
}
}
class Person {
public Person() {
System.out.println("wucan");
}
public Person(String name) {
this();
System.out.println("youcan");
}
}
Example11
public class Example11 {
public static void main (String[] ags)
{
Person p1=new Person();
Person p2=new Person();
p1=null;
p2=null;
System.gc();
for(int i=0;i<=100000;i++){
//延长代码运行时间
}
}
}
class Person {
public Person (){
System.out.println("对象将作为垃圾回收!");
}
}
Example12
public class Example12 {
public static void main (String[] ags)
{
Student stu1=new Student();
Student stu2=new Student();
Student.schoolname="传智播客";
System.out.println("我的学校是"+stu1.schoolname);
System.out.println("我的学校是"+stu2.schoolname);
}
}
class Student{
static String schoolname;
}
Example13
public class Example13 {
public static void main (String[] ags)
{
Person.sayHello();
}
}
class Person{
public static void sayHello(){
System.out.println("hello");
}
}
Example14
public class Example14 {
static {
System.out.println("测试类的静态代码被执行了!");
}
public static void main (String[] ags)
{
Person p1=new Person();
Person p2=new Person();
}
}
class Person{
static String country;
static{
country="china";
System.out.println("Preson中的静态代码被执行了!");
}
}
Example15
public class Example15 {
public static void main (String[] ags)
{
Single s1=Single.getInstance();
Single s2=Single.getInstance();
System.out.println(s1==s2);
}
}
class Single{
private static Single INSTANCE=new Single();
private Single(){
}; //声明其私有化,这样就不能在类的外部使用new关键字来创建对象了
public static Single getInstance(){
return INSTANCE;
}
}
Example16
class Outer
{
private int num=10086;
public void test(){
Inner inner=new Inner();
inner.show();
}
class Inner
{
public void show(){
System.out.println(num);
}
}
}
public class Example16
{
public static void main (String[] args)
{
Outer outer=new Outer();
outer.test();
}
}
Example17
public class Example17
{
public static void main (String[] args){
Outer.Inner inner=new Outer().new Inner();
inner.show();
}
}
class Outer
{
private int num=4;
public void test(){
Inner inner = new Inner();
}
class Inner
{
void show(){
System.out.println("num="+num);
}
}
}
Example18
public class Example18
{
public static void main(String[] args){
Outer.Inner inner=new Outer.Inner ();
inner.show();
}
}
class Outer
{
private static int num=666666;
static class Inner
{
void show(){
System.out.println("num="+num);
}
}
}
Example19
public class Example19
{
public static void main(String[] args){
Outer outer=new Outer ();
outer.test();
}
}
class Outer
{
private int num=4;
public void test ()
{
class Inner
{
void show(){
System.out.println("num="+num);
}
}
Inner in=new Inner();
in.show();
}
}