类与对象


在计算机中,程序是一段指令,其功能是解决问题。可以是一段代码,一个文件…
程序在运行时,就会产生一个进程.即进程是运行中对的程序。

如:System.out.print(“123”)
这行代码是一个程序,在运行时系统产生进程,分配资源和空间。

类与对象

:具有相同属性的一组对象对的集合,是抽象的。
对象:代码运行时,分配资源,开辟空间,产生对象。
对象之间相互独立。
类是对象的抽象,对象是类的实例。

例:

int []   a=new int[100]

代码中无对象,运行时产生对象,如上一行代码产生的能存储100个int数据的空间即为对象
a是对象名,通过对象名调用和编写代码

类的创建:

访问修饰符 类的标识符 类名{
        属性;
        方法;
}

例:

public class student{
        String name;
        void exam{
        System.out.print("123")
        }
}

类的初识化

1、利用构造方法
方法名与类名相同,无类型修饰符

public class Student {
	String name;
	int age;
	double stu_height;
	double stu_weight;
	public void BMI() {
		double BMI=stu_weight/stu_height/stu_height;
		if(BMI>=28) {
			System.out.println(name+"BMI为"+BMI+":肥胖");
		}
		else if(BMI>=24) {
			System.out.println(name+"BMI为"+BMI+":超重");
		}
		else if(BMI>=19) {
			System.out.println(name+"BMI为"+BMI+":标准");
		}
		else {
			System.out.println(name+"BMI为"+BMI+":偏瘦");
		}
		
	}
	//构造
	public Student(String name,double stu_height,double stu_weight) {
		this.name=name;
		this.stu_height=stu_height;
		this.stu_weight=stu_weight;
	}
	public static void main(String[] args) {
		Student stu1=new Student("张三",188,321);//构造方法初始化
		stu1.BMI();
	}
}

2.自定义初始化函数

public class Student {
	String name;
	int age;
	double stu_height;
	double stu_weight;
	public void BMI() {
		double BMI=stu_weight/stu_height/stu_height;
		if(BMI>=28) {
			System.out.println(name+"BMI为"+BMI+":肥胖");
		}
		else if(BMI>=24) {
			System.out.println(name+"BMI为"+BMI+":同学超重");
		}
		else if(BMI>=19) {
			System.out.println(name+"BMI为"+BMI+":同学体重标准");
		}
		else {
			System.out.println(name+"BMI为"+BMI+":同学偏瘦");
		}	
	}
	public void initlnfo(String name,double stu_height,double stu_weight) {
		this.name=name;
		this.stu_height=stu_height;
		this.stu_weight=stu_weight;
	}

	public static void main(String[] args) {
		Student stu2=new Student();
		stu2.initlnfo("ABC",188,321);
		stu2.BMI();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值