package com.lmy1;
/*类和对象的区别:
* 1、类是抽象的, 概念的 比如:猫 人
* 2、对象是具体的 实际 的
* 3、类是对象的抽象模板,对象是类的实体
*
* 4、定义一个类,类的首字母大写,方法名小写
* package 包名
* class 类名 extend 父类 implements
* 接口名
* {
* 成员变量(一般类型 或者 引用类<即类中引用类>)
*
* 构造方法(特点:1、方法名和类名相同;2、无返回值;
* 3、创建新类的时候,系统会自己调用该类的构造方法完成对象的初始化
* 4、一个类可以有多个构造方法;5、主要完成对象的初始化)
*
* 成员方法(人类的行为)
* public 返回数据类型 方法名(参数列表)
* {
* 语句;
* }
* }
*
*
* 5、this
*
*
* 6、类变量 和 类方法
* 类变量
* 是该类所有对象共享的变量,任何一个该类的对象物访问它的时候,取到的值是相同的
* 访问修饰符 static 数据类型 变量名
*
* 『类变量和实例变量的区别』
* 加Static变成类变量
* 类变量是公共属性,
* 实例变量是个体属性
* 类变量可以通过 类名.类变量名 直接访问
*
* 类方法:不能使用非静态变量
* 属于所有对象的实例
* 访问修饰符 static 数据返回类型 方法名()
* {
*
* }
*
* 7、四大特征
*
* 封装:(四大修饰符控制访问变量的范围)
*
* */
public class Demo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建猫的对象
Cat cat1 = new Cat();
//访问类的属性
cat1.Age = 20;
cat1.name = "mm";
cat1.color = "yollow";
//创建第二个猫类
Cat cat2 = new Cat();
//访问类的属性
cat2.Age = 10;
cat2.color = "bai";
cat2.name = "yy";
Person p1 = new Person();
p1.speak();
p1.cal(2);
System.out.println(p1.cal1());
Child c = new Child(8, "m");
Child b = new Child(5,"kk");
b.Join();
c.Join();
//静态方法的访问
Stu s1 = new Stu(12, "好", 50);
Stu s2 = new Stu(2, "和", 20);
System.out.println(Stu.gettfree());
//封装案例
Clerk clerk1 = new Clerk("经", 8, 90);
System.out.println(clerk1.getsal());
}
}
//写一个猫类
//1、类名首字母大写
class Cat
{
int Age;
String name;//也可以说String是引用类 因为String本身是一个封装类
String color;
Master myMster;//引用类
}
//主人类
class Master
{
int age;
String name;
String add;
}
//定义一个人类
class Person
{
int age;
String name;
//默认构造方法
public Person()
{
}
//构造方法
public Person(int age , String name)
{
age = age;
this.name = name;
}
//人的行为
//输出我是好人
public void speak()
{
System.out.println("我是好人");
}
//计算从1到100的和
public void cal(int a)
{
int res = 0;
for(int i = a; i<=10;i++ )
{
res+=i;
}
System.out.println(res);
}
public int cal1()
{
int res1 = 2;
int res2 = 3;
return res1+res2;
}
}
//类变量案例
class Child
{
int age;
String name;
static int n = 0;//n是静态变量, 可以被任何对象访问
public Child(int age, String name)
{
this.age = age;
this.name = name;
}
public void Join()
{
n++;
System.out.println("第"+ n +"孩子加入");
}
}
//类方法:
//学生类
class Stu
{
int Age;
String name;
int free;
static int tfree;
//构造函数
public Stu(int Age, String name, int free)
{
this.Age = Age;
this.name = name;
tfree+=free;
}
//返回总学费(静态方法,所以有对象都可以共享一个方法)
public static int gettfree()
{
//Age++;//静态的方法只能访问静态变量
//即类方法中不能访问非静态变量
return tfree;
}
}
//封装的简单案例
class Clerk
{
public String name;
//私有变量
private int Age;
private float sal;
//构造函数
public Clerk(String name , int age, float sal)
{
this.name = name;
this.Age = Age;
this.sal = sal;
}
//公有接口
public float getsal()
{
return this.sal;
}
}
/*类和对象的区别:
* 1、类是抽象的, 概念的 比如:猫 人
* 2、对象是具体的 实际 的
* 3、类是对象的抽象模板,对象是类的实体
*
* 4、定义一个类,类的首字母大写,方法名小写
* package 包名
* class 类名 extend 父类 implements
* 接口名
* {
* 成员变量(一般类型 或者 引用类<即类中引用类>)
*
* 构造方法(特点:1、方法名和类名相同;2、无返回值;
* 3、创建新类的时候,系统会自己调用该类的构造方法完成对象的初始化
* 4、一个类可以有多个构造方法;5、主要完成对象的初始化)
*
* 成员方法(人类的行为)
* public 返回数据类型 方法名(参数列表)
* {
* 语句;
* }
* }
*
*
* 5、this
*
*
* 6、类变量 和 类方法
* 类变量
* 是该类所有对象共享的变量,任何一个该类的对象物访问它的时候,取到的值是相同的
* 访问修饰符 static 数据类型 变量名
*
* 『类变量和实例变量的区别』
* 加Static变成类变量
* 类变量是公共属性,
* 实例变量是个体属性
* 类变量可以通过 类名.类变量名 直接访问
*
* 类方法:不能使用非静态变量
* 属于所有对象的实例
* 访问修饰符 static 数据返回类型 方法名()
* {
*
* }
*
* 7、四大特征
*
* 封装:(四大修饰符控制访问变量的范围)
*
* */
public class Demo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建猫的对象
Cat cat1 = new Cat();
//访问类的属性
cat1.Age = 20;
cat1.name = "mm";
cat1.color = "yollow";
//创建第二个猫类
Cat cat2 = new Cat();
//访问类的属性
cat2.Age = 10;
cat2.color = "bai";
cat2.name = "yy";
Person p1 = new Person();
p1.speak();
p1.cal(2);
System.out.println(p1.cal1());
Child c = new Child(8, "m");
Child b = new Child(5,"kk");
b.Join();
c.Join();
//静态方法的访问
Stu s1 = new Stu(12, "好", 50);
Stu s2 = new Stu(2, "和", 20);
System.out.println(Stu.gettfree());
//封装案例
Clerk clerk1 = new Clerk("经", 8, 90);
System.out.println(clerk1.getsal());
}
}
//写一个猫类
//1、类名首字母大写
class Cat
{
int Age;
String name;//也可以说String是引用类 因为String本身是一个封装类
String color;
Master myMster;//引用类
}
//主人类
class Master
{
int age;
String name;
String add;
}
//定义一个人类
class Person
{
int age;
String name;
//默认构造方法
public Person()
{
}
//构造方法
public Person(int age , String name)
{
age = age;
this.name = name;
}
//人的行为
//输出我是好人
public void speak()
{
System.out.println("我是好人");
}
//计算从1到100的和
public void cal(int a)
{
int res = 0;
for(int i = a; i<=10;i++ )
{
res+=i;
}
System.out.println(res);
}
public int cal1()
{
int res1 = 2;
int res2 = 3;
return res1+res2;
}
}
//类变量案例
class Child
{
int age;
String name;
static int n = 0;//n是静态变量, 可以被任何对象访问
public Child(int age, String name)
{
this.age = age;
this.name = name;
}
public void Join()
{
n++;
System.out.println("第"+ n +"孩子加入");
}
}
//类方法:
//学生类
class Stu
{
int Age;
String name;
int free;
static int tfree;
//构造函数
public Stu(int Age, String name, int free)
{
this.Age = Age;
this.name = name;
tfree+=free;
}
//返回总学费(静态方法,所以有对象都可以共享一个方法)
public static int gettfree()
{
//Age++;//静态的方法只能访问静态变量
//即类方法中不能访问非静态变量
return tfree;
}
}
//封装的简单案例
class Clerk
{
public String name;
//私有变量
private int Age;
private float sal;
//构造函数
public Clerk(String name , int age, float sal)
{
this.name = name;
this.Age = Age;
this.sal = sal;
}
//公有接口
public float getsal()
{
return this.sal;
}
}