例子1
package com.atguigu.java;
public class InstanceTest {
public static void main(String[] args) {
Phone p = new Phone();
p.sendEmail();
p.playGame();
new Phone().price=1999;
new Phone().showPrice();
PhoneMall mall = new PhoneMall();
mall.show(new Phone());
}
}
class PhoneMall{
public void show(Phone phone){
phone.sendEmail();
phone.playGame();
}
}
class Phone{
double price;
public void sendEmail(){
System.out.println("发送邮件");
}
public void playGame(){
System.out.println("玩游戏");
}
public void showPrice(){
System.out.println("手机价格为:"+price);
}
}
例子2
package com.atguigu.java1;
public class MethodArgsTest {
public static void main(String[] args) {
MethodArgsTest test = new MethodArgsTest();
test.show("hello");
test.show("hello","world");
test.show();
}
public void show(int i){
}
public void show(String ... str){
System.out.println("show(String ... strs)");
for(int i=0; i<str.length; i++){
System.out.println(str[i]);
}
}
}
例子3
package com.atguigu.java1;
public class ValueTransferTest {
public static void main(String[] args) {
Order o1 = new Order();
o1.orderId = 1001;
Order o2 = o1;
o2.orderId=11;
System.out.println(o1.orderId);
System.out.println(o2.orderId);
}
}
class Order{
int orderId;
}
例子4
package com.atguigu.java1;
public class AnimalTest {
public static void main(String[] args) {
Animal a = new Animal();
a.name="大黄";
a.age=1;
a.setLegs(-10);
a.show();
a.getLegs();
}
}
class Animal{
String name;
int age;
private int legs;
public void setLegs(int l){
if(l>=0 && l%2==0){
legs=l;
}else{
legs=0;
}
}
public int getLegs(){
return legs;
}
public void eat(){
System.out.println("动物进食");
}
public void show(){
System.out.println("name = "+name+",age = "+age+",legs = "+legs);
}
}
例子5
package com.atguigu.java1;
public class PersonTest {
public static void main(String[] args) {
Person person = new Person("王少锋");
System.out.println(person.name);
}
}
class Person{
String name;
int age;
public Person(){
System.out.println("Person()...");
}
public Person(String n){
name=n;
}
public void eat(){
System.out.println("人吃饭");
}
public void study(){
System.out.println("人可以学习");
}
}