java基础(4Day-方法-对象方法-类方法(static))

方法-对象方法-类方法(static)

方法:声明在类里面;
方法:
	1.访问属性: public/protected/private
	2.返回值:	void 基本数据类型 类
	3.方法名称
	4.参数类型: 基本数据类型 String 类 数组(集合)

复习get/set基本使用

在这里插入图片描述

成员变量私有(private),意味着只有类里面的方法(对象)才可以访问。

在这里插入图片描述

方法学习 - static

static 静态的:
	static 修饰方法: 叫类方法,它依赖于类存在,全局方法
	static 修饰成员变量
	static {	代码    }

static 修饰方法main

static修饰的方法静态方法,类方法,单例

如果static修饰的方法,静态方法,唯一,依赖类存在,类对象在Java虚拟机是唯一的。
java加载程序,static就会初始化

在这里插入图片描述
在这里插入图片描述

对象方法-没有static修饰

在这里插入图片描述
在这里插入图片描述

静态方法和对象方法练习

public class InputUtil {
	public int getInt(String s) {
		System.out.print(s);
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		return x;
	}
	public String getString(String s) {
		System.out.print(s);
		Scanner sc = new Scanner(System.in);
		String x = sc.next();
		return x;
	}
}
public class Sc {
	public static int getInt(String s) {
		System.out.print(s);
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		return x;
	}
	public static String getString(String s) {
		System.out.print(s);
		Scanner sc = new Scanner(System.in);
		String x = sc.next();
		return x;
	}
}

static修饰成员变量

在这里插入图片描述
在这里插入图片描述

static修饰成员变量

在这里插入图片描述

静态代码段

public class Sc {
    private static Scanner sc;
    static {
        System.out.println("静态代码段被执行了");
        sc = new Scanner(System.in);
    }
    public static int getInt(String msg ) {
        System.out.println(msg);
        //Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        return x;
    }
    public static String getString(String msg) {
        System.out.println(msg);
        //Scanner sc = new Scanner(System.in);
        String x = sc.next();
        return x;
	}
}

在这里插入图片描述

方法的返回值

方法:请求————响应的过程(本地)    (web)远程调用
    方法名称:请求响应的约定
返回值: 响应结果的(类型)
	void 	哑巴方法
	基本数据类型
	类

在这里插入图片描述
在这里插入图片描述

明白new作用

Book [name=敏姐Java, price=99.99, 作者:null]
Book [name=JQuery, price=55.55, 作者:null]
Book [name=JQuery, price=55.55, 作者:null]
//在添加一本 MySQL 66.66
//再显示出来
b = new Book("MySQL", 66.66);
shop.add(b);
shop.show();
//在添加一本 JQuery 55.55
//再显示出来
b.setName("JQuery");
b.setPrice(55.55);
shop.add(b);
shop.show();

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值