java基础--类、方法重载、构造器、对象

开启java学习之旅--基础部分

1.类--说明:类是由属性和方法构成的
    属性:又叫做特征,它是静态的、java中称作是一个字段或者域
    方法:又叫做行为,它是动态的、java又称为函数
    形式:关键字:class+类名+{}
2.方法:访问权限+返回值类型+()+{}
3.构造器:它的注意事项
/** * 构造器是创建对象用的 * 构造方法没有返回值 * 构造器的名字与类名一致 * 平常书写一般方法的时候注意,不要用类名 * 构造器不能被手动调用,而是在创建对象的时候自动调用 * 构造器通常是用来给成员变量赋值用的 * 如果某个类没有定义构造,编译器认为有一个公有的默认的无参空构造 * 即:编译器认为有 public 类名(){} * 当你有构造器之后,编译器认为你知道如何去创建一个对象, * 不再提供无参的空构造。 * 在构造器中调用另一个构造器,该语句写在第一行 */
4.对象:思想--万物皆对象--需要什么就
其实光看定义的话,新手可能觉得有些抽象,话不多少,上代码(代码可以自己复制去调试)
1.类+方法--创建一个人类及人类有的一些方法
        --注意创建类和方法的形式
        --有的人可能会疑问为什么不写访问权限?public or private?那是因为不写的话编辑器会默认为public的
class People{
	String name;
	int age;
	char gender;
	/**
	 * 人会吃,人有吃的行为--eat方法
	 */
	public void eat(){
		System.out.println("吃好吃的");
	}
	/**
	 * 人会睡,人有睡的行为--sleep方法
	 */
	public void sleep(){
		System.out.println("睡饱饱的");
	}
}

2.方法的重载--对照着注释进行学习

       /**
	 * 方法重载:参数名相同,列表不同(参数的个数不同、参数的类型不同、参数的顺序)
	 */
	public void study(int hours) {
		System.out.println("我要学习" + hours + "小时");
	}

	public void study(String subject) {
		System.out.println("我要学习" + subject);
	}

	public void study(int hours, String subject) {
		System.out.println("我要学习" + subject + hours + "小时");
	}

	/**
	 * 构造方法:与类同名,无返回值类型,方便创建对象 构造方法的重载
	 */
	public Student() {
		System.out.println("无参构造方法");
	}

	public Student(int age, String name) {
		System.out.println(name + "有参构造方法" + age);
	}
}

3.构造器和构造器的重载

public class Student {

	String name;
	long id;
	int age;
	Bag bag;
	public Student(){
		this("方文奇");
		System.out.println("无参构造被调用了");
	}
	
	/**
	 * 构造器重载
	 */
	public Student(String name){
		this(name, 25);
		System.out.println("一个参数的构造被调用了");
		//this.name = name;   // self
	}
	
	/**
	 * 构造器重载
	 */
	/*public Student(String stuName, int stuAge){
		name = stuName;
		age = stuAge;
	}*/
	public Student(String name, int age){
		System.out.println("两个参数的构造被调用了");
		this.name = name;
		this.age = age;
	}
	
	public void study(){
		System.out.println(name + "Say:Good good study, day day up!");
		this.play();
	}
	
	/**
	 * 重载一个study方法
	 * 重载(overload)的概念:在同一个类中,或者父子类中,方法名相同,而形参列表不同多个方法形成重载。
	 * 访问权限修饰符,返回值类型,并不影响重载
	 * 形参列表不同具体又分为:
	 * 形参列表的个数不同
	 * 形参列表的数据类型不同
	 * 方法名不同不叫重载
	 * 
	 * @param hours
	 */
	public void study(int hours){
		System.out.println("我要学习" + hours + "小时");
	}
	
	/**
	 * 重载一个study方法,注意类型
	 */
	public void study(String subject){
		System.out.println("我在学习" + subject);
	}
	
	/**
	 * 重载一个study方法,注意个数
	 */
	public void study(String subject, int hours){
		System.out.println("我在学习" + subject + ",我要学" + hours + "小时");
	}
	
	/**
	 * 重载一个study方法,注意顺序
	 */
	public void study(int hours, String subject){
		System.out.println("我要学" + hours + "小时的" + subject);
	}
	
	public void talk(){
		System.out.println("I think i can, so i can");
	}
	
	public void play(){
		System.out.println(this.name + "说,学累了玩会儿,劳逸结合");
	}
	
	public String toString(){
		this.bag = new Bag();
		return "姓名:"+this.name+",年龄:"+age +",我有个包:"+this.bag;
	}
	
}	

4.对象

int age=20;
public void eat(){
}
public static void main(String[] args) {
Student s1 = new Student();
s1.age;//调用参数age
s1.eat();//调用eat()方法
}
用Student类去创建一个名字为s1的对象(开辟了一块空间,s1指向这块空间的地址),使用对象是为了更好的去调用方法或者参数

5.实战--写一个英雄类和一个野怪类,英雄和野怪都有自己属性,然后让英雄和野怪进行pk

(读者在都代码时可以将英雄和野怪都各自创建一个类,这里是粘杂一起,方便对比查看学习)
//英雄类和野怪类
import java.util.Scanner;
public class demo1{	
	Scanner scanner = new Scanner(System.in);
	public void Attack() {
		String choice=scanner.nextLine();
		
		if (choice.equals("y")) {
			System.out.println("Win!剩余血量为:2600");
		}else {
			System.out.println("已逃跑、、、");
		}
		
	}
	
}
// 技能类
class Skill {
	String skill;
	boolean flag;
	String xiaoguo;
	String shanghai;

	public Skill(String skill, boolean flag, String xiaoguo, String shanghai) {
		this.skill = skill;
		this.flag = flag;
		this.xiaoguo = xiaoguo;
		this.shanghai = shanghai;
	}
	public String toString() {
		return skill + " " + (flag ? "主动" : "被动") + " " + "效果是:" + xiaoguo
				+ " 伤害是:" + shanghai;
	}
}

class Hero {
	String name;
	char sex;
	int id;
	int blood;
	Skill[] skill;

	public Hero(String name, char sex, int id, int blood) {
		this.name = name;
		this.sex = sex;
		this.id = id;
		this.blood = blood;
	}

	public void initSkill() {
		skill = new Skill[5];
		skill[0] = new Skill("我的1技能是:沉重打击", true, "沉默", "300");
		skill[1] = new Skill("我的2技能是:无畏之盾", true, "坚韧", "0");
		skill[2] = new Skill("我的3技能是:无敌旋转", true, "打击", "500");
		skill[3] = new Skill("我的4技能是:致命之剑", true, "杀人", "1000");
		skill[4] = new Skill("我的被动是 :日炎灼烧", false,"灼烧", "200");
	}

	public void show() {
		System.out.println("嘿,boy! my name is " + this.name + ",我是一个"
				+ this.sex + "人,我的id是" + this.id + ",我的血量是:" + blood);
		initSkill();
		for (int i = 0; i < skill.length; i++) {
			System.out.println(skill[i]);
		}
	}

}

class yeguai {

	String name;
	int attack;
	int blood;

	public yeguai(String name) {
		this.name = name;
	}

	public void initAttack() {
		attack = 600;
		blood = 2200;

	}

	public void show() {
		System.out.println("野怪的名字为:" + this.name);
		initAttack();
		System.out.println("魔爪蛙的攻击力为:" + this.attack + ",它的血量为:" + blood);
	}

}

//测试

public class Test {
	public static void main(String[] args) {	
		System.out.println("英雄简介:");
		Hero hero=new Hero("盖伦",'女',2019,5000);
		hero.show();
		System.out.println("---------------------------------------");
		System.out.println("怪物简介:");
		yeguai ye=new yeguai("魔爪蛙");
		ye.show();
		System.out.println("---------------------------------------");
		System.out.println("是否进行攻击?(y or n)");
		demo1 de=new demo1();
		de.Attack();
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值