Java学习记录 类和对象

对象

对象 就是通过java类所产生的实体
如:动物百科的记载中有兔子类(java封装的类),根据百科中所呈现出来且真实存在的 实体 被为对象,相当于得到了兔子的 对象

类 是封装 对象 的 行为 和 属性 的载体。反过来,具有 相同属性 和 行为 的一类实体被称为类
如:兔子类(所有物种的兔子)封装有兔子的 共同属性 和 行为

封装

封装是将类的某些信息隐藏在类的内部,不许外部直接访问
通过类提供的方法实现对 隐藏信息 的 访问 和 操作
如:QQ聊天的界面,聊天的内容位置是已经提供方法,可 访问 和 操作 的,可避免其他操作影响的不良后果

继承

继承 是类与类之间同样具有关系,则为两类的关联
如:学生类 与 教师类 关联关系

多态

多态 是父类对象应用于子类的特征

成员变量

成员变量是java类中的属性

public class tuzi {
    
    
    public static void main(String[] args) {
    
    
        String eat;     //吃
        String hair;    //毛
        String jump;  	//跳
    }
}

成员方法

成员方法是java中的行为

权限修饰符 返回值类型 方法名(参数类型 参数名[,···){
    
    
    ····		//方法体
    return 返回的值;
}
//例子
public int max(int a , int b){
    
    
    if(a > b){
    
    
        return a;
    }else{
    
    
        return b;
    }
}

如果方法返回类型为void(空),则无返回(retrun)

权限修饰符

权限修饰符控制 类 、类的成员变量 、成员方法 的访问

类修饰符 本类 同包其他类 / 子类 其他包的类 / 子类
private 可见 不可见 不可见
protected 可见 可见 不可见
public 可见 可见 可见

当声明类时不使用修饰符设置权限 ,则这个类预设为包存取范围,前提是只有一个包中的类可以调用类的成员方法 、成员变量

使用默认修饰符的例子

//AnyClass类为默认访问权限
class AnyClass {
    
    
    //doString()方法访问权限为public
	public void doString(){
    
    
		····	//方法体
	}
}

局部变量

成员方法内定义的变量,则该变量是局部变量

public class jububianliang {
    
    
	public static void main(String[] args) {
    
    
		int i = 2,j = 4;
		System.out.private(max(i,j));	
	}
    //成员方法。max变量限于该方法的范围
	public int max(int a , int b ){
    
    
        //局部变量max
		int max = 0;
		if(a > b){
    
    
			max = a;
			return max;
		}else{
    
    
			max = b;
			return max;}
	}
}

方法内的局部变量,外部是无法调用方法内的局部变量,因外部并没有声明变量

this关键子

this关键字用于引用对象的成员变量和方法 ,将方法的参数值赋予类本身的成员变量

public class thsiches {
    
    
    String  str = "abc";
    public void no1(String str) {
    
    
        //打印 str形参
        System.out.println("no1:"+str);
        //打印 thsiches类 中的str成员变量
        System.out.println("no2:"+this.str);
    }
    public static void main(String[] args) {
    
    
        String str2 = "123";
        new thsiches().no1(str2);
    }
}

类的构造方法

构造方法是创建对象过程中运行的方法,也是初始化方法,与类同名,对象创建是通过构造方法完成的

构造方法

  • 没有返回值,也不需void(空)修饰
  • 名称要与本类的名称相同
  • 没有定义构造方法,编译器会自动创建无参数构造方法
  • 实例化对象,类都会自动调用构造方法
  • 在无参调用 this 有参构造方法,则该语句必须为第一个
  • 实例化对象是有参构造方法,参数类型必须对应相应的值,否则会报错
public static void main(String[] args) {
    
    
    System.out.println("1");
    //实例化对象
    no1 a = new no1("123");
    System.out.println("2");
    //实例化对象
    no1 b = new no1();
    System.out.println("3");
}
//无参构造方法
public no1() {
    
    
//使用this调用有参构造方法
    this("this 调用1");
    System.out.println("无参构造方法");
}
//有参构造方法
public no1(String str){
    
    
    this(123);
    System.out.println(str);
}
public no1(int i){
    
    
    System.out.println("this 调用2");
}

运行结果

1
this 调用2
123
2
this 调用2
this 调用1
无参构造方法
3

私有构造方法

书本类

public class Book {
    
    
	//私有构造方法
	private Book(){
    
    
        ···
    }
    //静态公开方法,图书馆借书
    static public Book libraryBorrow(){
    
    
        return new Book();
    }
}

借书人类

public class Mytext{
    
    
    public static viod main(String[] args){
    
    
    //创建一个书的对象,不是new实例化的,而是通过方法从图书馆借来的
		Book book = Book.libaryBorrow();
        //错误方法	Book book = new Book();
    }
}

static 静态

静态修饰的代码,整个程序运行结束之后才会释放

静态区的数据是共享的,其他类调用本类的静态方法和静态变量时,无需实例化调用

类名.静态类成员

静态变量

静态变量的共享,在不同类对同一变量可以进行操作

public class no1 {
    
    
    //静态变量
    static public int i = 0;
    public void out(){
    
    
        if(i>=3){
    
    
            i -=3;
        }else{
    
    
            i = 0;
        }
    }
    public void inlet(){
    
    
        i+=4;
    }
    public static void main(String[] args) {
    
    
        no1 out = new no1();
        no1 in = new no1();
    
        System.out.println(i);
        in.inlet();
        System.out.println("in:"+i);
        out.out();
        System.out.println("out:"+i);
        out.out();
        System.out.println("out:"+i);
        
        no11 n = new no11();
    
    }
}

//外部类调用静态变量
class no11 {
    
    
    public no11(){
    
    
        //调用no1类的方法
        new no1().inlet();
        System.out.println("no11(in) :  "+no1.i);
    }
}

运行结果

 0
in:4
out:1
out:0
no11(in) :  4

同一个类的不同实例对象, 共用同一静态变量,如果一个对象将其更改,另一个对象的静态变量也会更改

静态常量

静态常量用 final static 修饰成员变量

public class no2 {
    
    
    //静态常量
    final static double P = 3.1415;
    
    public static void main(String[] args) {
    
    
        Circular c = new Circular(2);
    }
}
//计算圆类
class Circular{
    
    
    double r;
    double a;
    public Circular(double r){
    
    
        this.r = r;
        a = no2.P * r * r;
        System.out.println("圆半径r为:"+r);
        System.out.println("圆面积a为: "+a);
    }
}

运行结果

圆半径r为:2.0
圆面积a为: 12.566

静态方法

静态方法无需创建类的对象

public class no3 {
    
    
    //静态方法
    static public void show(){
    
    
        System.out.println("以调用静态方法");
    }
    
    public static void main(String[] args) {
    
    
        no3.show();
    }
}
  • 在静态方法中不可以使用 this关键字
  • 在静态方法中不可以直接调用非静态方法

类的主方法

主方法是类的入口点,也是程序运行的开始

public static void main(String[] args) {
    
    }
  • 主方法是静态的,主方法调用其他方法也必须是静态
  • 主方法无返回值
  • 主方法的形参为数组,args数组代表里面的参数个数,也可用args.length 获取参数个数

对象

对象创建

Text text = new Text();
Text text = new Text("a");

Text:类名
text:创建Text类引用对象
new:创建对象操作符
“ a ”:构造方法的参数

对象属性与行为

使用new操作符创建对象后,可以使用 对象.类成员 获取对象的 属性 或 行为

public class no1 {
    
    
    int i = 50;
    public void max(int a,int b){
    
    
        System.out.println("调用no1类的max方法(no1.max)");
        if(a > b){
    
    
            System.out.println("max = a"+a);
        }else{
    
    
            System.out.println("max = b"+b);
        }
    }
    public static void main(String[] args) {
    
    
        //对象的创建
        no1 t1 = new no1();
        no1 t2 = new no1();
        
        //获取对象的属性(变量)
        t2.i = 60;
        System.out.println("t2对象i:"+t2.i);
        System.out.println("t1对象i:"+t1.i);
        System.out.println("t1对象i:"+t1.i++);
    
        System.out.println();
        //调用t1对象max方法
        t1.max(12,23);
        //调用t2对象max方法
        t2.max(22,33);
    }
}

运行结果

t2对象i:60
t1对象i:50
t1对象i:50

调用no1类的max方法(no1.max)
max = b23
调用no1类的max方法(no1.max)
max = b33

对象引用

Text t = new Text();

Text:类
t:引用
new Book():对象

扫描器

扫描器用来获取用户输入的内容

使用方式

//实例化
Sanner sc = new Sanner(System.im);
//获取变量调用的方法
double d = sc.nextDouble();
int i = sc.nextInt();
···

时间记录

记录开始运行到运行以下代码的时间段

long i = System.currentTimeMillis();

猜你喜欢

转载自blog.csdn.net/weixin_45963193/article/details/107303093