DAY04每日作业:
int[] array2 = new int[10];//动态初始化
int[] array1 = new int[]{2,3,5,7,11,13,17,19};//静态初始化
for (int i=0; i<8; i++)
{
System.out.println(array1[i]);
array2[i] = array1[i];
if (i%2==0)
{
array2[i] = i;
}
}
今天开始,我们就要学习面向对象编程了。
1.面向对象概述:
OOP
三大特征:封装、继承、多态
万物皆对象
2.类的写法:
我要开车去丽江。
类:我、车;
方法:我{ 开车(丽江){} 车 {启动(){} 停止(){} 前进(){}}
类的语法格式:
/*
修饰符 class 类名{
属性声明;
方法声明;
}
说明:修饰符public,类可以被任意访问
类的正文要用{}括起来
*/
public class Person
{
public int age;
public void showAge()
{
System.out.println(age);
}
}
人的类:
package hello;
/**
* 人
* @author Daniel
*
*/
public class Person
{
//属性,成员变量,可以先声明,不用初始化
String name;//默认值为null
int age;//默认值为0
//行为,方法,也叫函数
/**
* 打印姓名
*/
public void showName()//驼峰命名,xxxYyyZzz
{
System.out.println("姓名:"+name);
}
/**
* 获取年龄
* @return
*/
public int getAge()
{
return age;
}
}
动物的类:
package hello;
/**
* 动物的类
* @author Daniel
*
*/
public class Animal
{
String name;//动物名称
int eyes;//眼睛数
int legs;//腿数
/**
* 输出动物吃的
* @param food 食物
*/
public void eat(String food)
{
System.out.println("此种动物的食物为:" + food);
}
/**
* 动物的移动方式
* @param moveType 移动方式
*/
public void move(String moveType)
{
System.out.println("此种动物的移动方式为:" + moveType);
}
}
3.类的实例化-对象:
程序员没有对象没关系,new一个就行。
package hello;
public class Test1 extends Animal
{
public static void main(String[] args)
{
//实例化Person类,创建Person对象
Person person = new Person();//声明一个Person类型的变量,名为person,new Person就是实例化并赋给变量person
person.name = "张三";//给person对象的name属性赋值
person.showName();//对象方法的调用
int i = person.getAge();
System.out.println(i);
//实例化一个Animal对象
Animal ani = new Animal();
ani.eat("shit");
ani.move("fly");
}
}
4.类的属性:
语法格式:修饰符 类型 属性名 = 初值
修饰符private:只能由该类使用;
修饰符public:都可用。
public class Person()
{
private int age;
public String name = "Lisa";
}
static,意思是静态的,这样的变量不需要实例化,直接可以类名.属性这样的方式直接调用。
5.类的方法:
课堂内小测:
答:
package hello;
/*
* 学生类
*/
public class Student
{
public String name;
public int age;
public String attendence;
public String interest;
public void showInfo()
{
System.out.println(name + "年龄:" + age + " 参加课程:" + attendence + " 兴趣:" + interest);
}
}
另个Teacher类同理。
调试:
package hello;
public class Test2
{
public static void main(String[] args)
{
Student stu = new Student();
stu.name = "张三";
stu.age = 18;
stu.attendence = "老罗";
stu.interest = "法外狂徒";
stu.showInfo();
}
}
如果在同一个类中,类可以直接调用,不用new实例化
6.关于对象:
初始值
匿名对象:
当这个对象只使用一次,之后不管了,就采用 匿名对象
Student s = new Student();
s.showInfo();
//与下一条语句作用相同
new Student().showInfo();
随堂练习:
package hello;
/**
* Person 类
* @author Daniel
*
*/
public class Person
{
public String name;
public int age;
public int sex;
/**
* study方法
*/
public void study()
{
System.out.println("studying");
}
/**
* showAge方法
*/
public void showAge()
{
System.out.println("年龄" + age);
}
/**
* 年龄增加
* @param i 传入参数
* @return
*/
public int addAge(int i)
{
age += i;
return age;
}
}
package hello;
/**
* 测试输出类
* @author Daniel
*
*/
public class Test3
{
public static void main(String[] args)
{
Person person = new Person();
person.name = "张三";
person.age = 19;
person.study();
person.age = person.addAge(2);
System.out.println(person.age);
}
}
类中方法可以直接访问类中变量
落地法则:
1.关于类的设计,即成员的属性和方法;
2.类的实例化,new 一个对象;
3.通过 对象.属性、对象.方法执行。
7.方法的重载:
//多个同名方法在一个类中共存,必是参数类型或者个数不同
public int add(int x, int y){
return x*y;
}
public int add(int x, double y){
return x*y;
}
public int add(int x, int y, int z){
return x*y;
}
练习:
package hello;
/**
* 计算类
* @author Daniel
*
*/
public class Culculate {
public int x;
public int y;
public String str;
/**
* 平方方法
* @param x
*/
public void mOL(int x)
{
System.out.println(x*x);
}
/**
* 乘法方法
* @param x
* @param y
*/
public void mOL(int x,int y)
{
System.out.println(x*y);
}
/**
* 输出字符串方法
* @param str
*/
public void mOL(String str)
{
System.out.println(str);
}
}
package hello;
/**
* 主函数调用
* @author Daniel
*
*/
public class Main {
public static void main(String[] args)
{
Culculate cul = new Culculate();
cul.mOL(2);
cul.mOL(2, 4);
cul.mOL("go");
}
}