java基础--面向对象(上)

例子1

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 20:19
 */
public class InstanceTest {
    public static void main(String[] args) {
        Phone p = new Phone();

        p.sendEmail();
        p.playGame();

        //匿名对象,这下面两个调用的不是同一个对象
//        new Phone().sendEmail();
//        new Phone().playGame();

        new Phone().price=1999;
        new Phone().showPrice();  //0.0

        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;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 20:58
 */
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 s){
//
//    }

    //下面这个是可变个数的形参
    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;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 21:20
 */
public class ValueTransferTest {

    public static void main(String[] args) {

        Order o1 = new Order();
        o1.orderId = 1001;

        Order o2 = o1;   //这里o1,o2指向同一个实体

        o2.orderId=11;

        System.out.println(o1.orderId);
        System.out.println(o2.orderId);


    }
}

class Order{

    int orderId;

}

例子4

package com.atguigu.java1;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 17:24
 */
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;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:00
 */
public class PersonTest {

    public static void main(String[] args) {
        Person person = new Person("王少锋");  //初始化属性 
        
        // 再new的时候,就已经调用空参的构造器了
        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("人可以学习");
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值