面向对象2

1、在Java中不可能直接操作对象本身,所有的对象都由一个引用指向,必须通过这个引用才能访问对象本身,包括获取成员变量的值,改变对象的成员变量,调用对象的方法等。

2、Integer包装类,属于引用数据类型。基本数据类型不能用instanceof判断类型

package EXERCISE;
public class ceshi {
    public static void main(String[] args) {
        String str="abcde";    
        if(str instanceof String){
            System.out.println("str是字符串");
        }
        Integer i=10;
        if(i instanceof Integer){
            System.out.println("i是整数");
            
        }
    }

}

3、一个类中可以有多个类,但只能有一个public修饰的类,且只能运行public修饰的类。

package EXERCISE;
//Student类
class Student {
    public void study(){
        System.out.println("我是一个学生");
    }

}
//Teacher类
//如果方法的返回值是类名:其实返回的该类的对象
class Teacher {
    public Student get(){
        Student stu=new Student();
        return stu;        
    }
}

public class Test {
    //通过Teacher得到Student对象,然后调用Student类的方法
    public static void main(String[] args) {
        Teacher t=new Teacher();
        Student s=t.get();
        s.study();

    }

}
package LESSON5;
import java.util.*;

class Vehicle {
    public int speed;
    public String brand;
    public Vehicle(int speed,String brand){
        this.speed=speed;
        this.brand=brand;
        this.move();
    }
    public void move(){
        System.out.println(brand+"当前速度为"+speed+"码");
        
    }
    public void speedUp(int speedUp){
        speed+=speedUp;
        System.out.println("加速"+speedUp+"当前速度为"+speed);
        
    }
    public boolean speedDown(int speedDown){
        if(speedDown<=speed){
        speed-=speedDown;
        System.out.println("减速"+speedDown+"当前速度为"+speed);
        return true;
        }
        else{
            speed=0;
            System.out.println("减速速度已达最大,车已停止");
            return false;
        }
    }
    public void stop(){
        speed=0;
        System.out.println("已停车");
    }
}

public class TestVehicle {
    public static void main(String[] args) {
        Vehicle ve=new Vehicle(80,"奥迪");
        Scanner sc=new Scanner(System.in);
        while(true){
        System.out.println("请选择:1加速 2减速 3停车");
        int x=sc.nextInt();
        
        if(x==1){
            System.out.println("加速:");
            int up=sc.nextInt();
            ve.speedUp(up);}
        else if(x==2){
            System.out.println("减速:");
            int down=sc.nextInt();
//            ve.speedDown(down);//不能在此处再调用speedDown()方法,否则减速会减两次。下面判断是否为false时已经调用了,不必再调用。
            if(ve.speedDown(down)==false){
                break;
                }          
    }
        else if(x==3){
            ve.stop();
            break;
            
            
        }
        else{
            System.out.println("输入错误");
        }
        
    }
        

}
}

4、两种方法

package LESSON5;
import java.util.*;

//一个类中可以有多个类,但只能有一个public修饰的类,且只能运行public修饰的类
 class A {
    public int v=100;
    public boolean guess(int x){
        
        if(x>v){
            System.out.println("猜大了");return false;
        }else if(x<v){
            System.out.println("猜小了");return false;
            
        }
        else{
            System.out.println("猜测成功");return true;
            
        }                        
    }
}

public class TestA {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        A a=new A();        
        while(true){
            System.out.println("猜数字游戏,请输入数字: ");
            int x=sc.nextInt();
            if(a.guess(x)){
                break;
                
                
            }
            
        }
        

    }

}
package LESSON5;
import java.util.*;

class B{
    public int v=100;
    public void guess(){
        Scanner sc=new Scanner(System.in);
        while(true){
            System.out.println("猜字谜游戏,请输入:");
            int a=sc.nextInt();
            if(a>v){
                System.err.println("猜大了");continue;
                
            }else if(a<v){
                System.out.println("猜小了");continue;
                
            }else{
                System.out.println("猜对了");break;
            }
            
        }
    }
}

public class TestA2 {
    public static void main(String[] args) {
        B b=new B();
        b.guess();

    }

}
原文地址:https://www.cnblogs.com/qfdy123/p/10939619.html