package ooday01;
/*
学生类
*/
//只能写public class ;里面 Students类中 属性 构造方法 (标准的一般有无参的构造 有参的 )方法
public class Students {
//同一个类中直接返回name
String name;
int age;
String className;
String stuId;//方法外边的叫成员变量
//this.在给成员变量赋初值时用,
//java规定:成员变量和局部变量是可以同名的,使用的时候默认采取就近原则
//局部变量(当前方法中)String name1, int age1, String className1, String stuId1这些
Students(){
}
Students(String name,int age){
this.name = name;
this.age = age;
}
Students(String name, int age, String className, String stuId) {
// name1;指的是局部的,String name1,所以写成this.name 就是
//当成员变量 与 局部变量 同名,不可以省this
this.name = name;//局部是黑色,紫色是成员变量 name = name这种方法是不行的
this.age = age;
this.className = className;
this.stuId = stuId;
}
//}
/*
class Students {
//同一个类中直接返回name
String name;
int age;
String className;
String stuId;
*/// 方法
void study(){
System.out.println(this.name+"在学习...");
}
//this.name;
void sayHi(){//this指的是张三
System.out.println("大家好,我叫" + this.name + ",今年" + this.age + "岁了,所在班级" + this.className + "学号为" + this.stuId);
}
void playWith(String anotherName){
System.out.println(this.name+"正在和"+anotherName+"一起玩...");
}
//anotherName 前面不带this. 只有在方法的里面用
}
package ooday01;
/**
*
*/
public class StudentTest {
public static void main(String[] args) {
Students zs = new Students("zs",25,"jsd2302","001");
System.out.println(zs);
//数据类型 引用类型(变量) =是指向 对象// new Student();这样用不了,要接受
1)创建了一个学生对象//2)给所有成员变量赋默认值/ /3)调用student的构造方法
//student ww = new Tstudent(
/*
Students zs = new Students();//声明一个Student类型引用zs指向了一个学生对象,zs就代表学生对象.访问就打点
//int[] arr = new int[10];
zs.name = "张三";
zs.age = 24;
zs.className = "jsd2302";
zs.stuId = "001";//凡是类里面的都直接用
zs.study();//无参硬是传参.用参时会报错
zs.sayHi();
zs.playWith("lisi");
Students lisi = new Students();
lisi.name = "lisi";
lisi.age = 25;
lisi.className="jsd2302";
lisi.stuId = "002";
lisi.study();
lisi.sayHi();
lisi.playWith("zs");//引用类型的默认值为null
//byte short int char long -0
//double float =-----------0.0
//String ------------------null
//引用类型------------------null
Students ww = new Students();
ww.sayHi();
ww.study();
ww.playWith("zs");
*/
}
}
package ooday01;
/*
构造方法演示
*/
public class ConsDemo {
public static void main(String[] args) {
Students zs = new Students();//调用无参构造方法
Students ls = new Students("ls",24);//调用2个构造方法
Students ww = new Students("ww",26,"jsd2302","001");//调用四个构造方法
zs.sayHi();
ls.sayHi();
ww.sayHi();
}
}
public class创建类,new 关键字创建对象,打点调用,关键还是类的创建,然后就是this.只有在紫色的是成员变量,黑色的是局部变量,
this指代当前对象,哪个对象调用方法它指的就是哪个名字
给成员变量赋初值,与类同名,没有返回值类型,连void也没有,创建对象时被自己调用.
package ooday02;
public class Car {
String brand;
String color;
double price;
Car(){//无参构造方法
}
//有参的构造方法
Car(String brand,String color,double price){
this.brand = brand;
this.color = color;
this.price = price;
}
void start(){
System.out.println("品牌是"+brand+"颜色是"+color+"价格是"+price+"的车正在启动中");
}
void run(){
System.out.println("品牌是"+brand+"颜色是"+color+"价格是"+price+"的车正在启动中");
}
void stop(){
System.out.println("品牌是"+brand+"颜色是"+color+"价格是"+price+"的车正在启动中");
}
/*
String brand;
String color;
double price;
Car(){
}
Car(String brand,String color,double price){
this.brand = brand;
this.color = color;
this.price = price;
}
void start(){
System.out.println("品牌是"+brand+"颜色是"+color+"价格是"+price+"的车正在启动中");
}
void run(){
System.out.println("品牌是"+brand+"颜色是"+color+"价格是"+price+"的车正在启动中");
}
void stop(){
System.out.println("品牌是"+brand+"颜色是"+color+"价格是"+price+"的车正在启动中");
}
*/
}
package ooday02;
public class carTest {
public static void main(String[] args) {
Car baoma =new Car();
//无参构造,什么也不做,就赋值.这里疏忽了赋值
baoma.price = 200;
baoma.color = "白色";
baoma.brand = "宝马";
baoma.start();
baoma.run();
baoma.stop();
Car benci = new Car("benci","白色",400);
benci.start();
benci.run();
benci.stop();
/*
Car baoma =new Car();
baoma.price = 200;
baoma.color = "白色";
baoma.brand = "宝马";
baoma.start();
baoma.run();
baoma.stop();
Car benci = new Car("benci","白色",400);
benci.start();
benci.run();
benci.stop();
*/
}
}
package ooday02;
/*
结构体的创建以及演示
*/
public class ConsDemo {
public static void main(String[] args) {
Student zs = new Student();调用无参构造方法,然后进行赋值
zs.classid="101";
zs.age = 23;
zs.name = "zhangsan";
zs.className = "JSd2023";
zs.sayHi();
zs.study();
zs.playWith("lisi");
//调用四个构造方法
Student ww = new Student("ww","jsd2302",24,"001");//调用四个构造方法
/*
Student zs = new Student();调用无参构造方法,然后进行赋值
zs.classid="101";
zs.age = 23;
zs.name = "zhangsan";
zs.className = "JSd2023";
zs.sayHi();
zs.study();
zs.playWith("lisi");
//调用四个构造方法
Student ww = new Student("ww","jsd2302",24,"001");//调用四个构造方法
*/
}
}
package ooday02;
/*
student的类
Student、ConsDemo、Car、CarTest
作业内容:
成员变量
1. 完成`Student、ConsDemo、Car、CarTest类的-----------每人最少两遍
*/
public class Student{
String name;
String className;
int age;
String classid;
Student(){
}
Student(String name,String className,int age,String classid){
this.name = name;
this.className = className;
this.age = age;
this.classid = classid;
}
void study(){
System.out.println("名字"+name+"教室"+className+"学号"+classid+"年龄"+age+"正在学习");
}
void sayHi(){
System.out.println("大家好,我叫" + this.name + ",今年" + this.age + "岁了,所在班级" + this.className + "学号为" + this.classid);
}
void playWith(String anotherName){
System.out.println(this.name+"正在和"+anotherName+"一起玩...");
}
}
/*
public class Student {
String name;
String calssName;
int age;
String classid;
//定义无参的构造方法
Student(){
}//定义有参的构造方法
Student(String name,String calssName,int age,String classid){
this.name = name;
this.calssName = calssName;
this.age = age;
this.classid = classid;
}
void study(){
System.out.println("名字"+name+"教室"+calssName+"学号"+classid+"年龄"+age+"正在学习");
}
}
*/
package ooday02;
/*
创建对象,你用new关键字
*/
public class StudentTest {
public static void main(String[] args) {
Student zs = new Student();
zs.age = 24;
zs.className = "jsd2023";
zs.classid = "101";
zs.name = "zhangsan";
zs.study();
Student zd = new Student("zhangsan","jsd2023",23,"102");
zd.study();
zd.sayHi();
zd.playWith("lisi");
/*
Student zs = new Student();
zs.age = 24;
zs.className = "jsd2023";
zs.classid = "101";
zs.name = "zhangsan";
zs.study();
Student zd = new Student("zhangsan","jsd2023",23,"102");
zd.study();
*/
}
}
package ooday01;
/*
学生类
*/
//只能写public class ;里面 Students类中 属性 构造方法 (标准的一般有无参的构造 有参的 )方法
public class Students {
//同一个类中直接返回name
String name;
int age;
String className;
String stuId;//方法外边的叫成员变量
//this.在给成员变量赋初值时用,
//java规定:成员变量和局部变量是可以同名的,使用的时候默认采取就近原则
//局部变量(当前方法中)String name1, int age1, String className1, String stuId1这些
Students(){
}
Students(String name,int age){
this.name = name;
this.age = age;
}
Students(String name, int age, String className, String stuId) {
// name1;指的是局部的,String name1,所以写成this.name 就是
//当成员变量 与 局部变量 同名,不可以省this
this.name = name;//局部是黑色,紫色是成员变量 name = name这种方法是不行的
this.age = age;
this.className = className;
this.stuId = stuId;
}
//}
/*
class Students {
//同一个类中直接返回name
String name;
int age;
String className;
String stuId;
*/// 方法
void study(){
System.out.println(this.name+"在学习...");
}
//this.name;
void sayHi(){//this指的是张三
System.out.println("大家好,我叫" + this.name + ",今年" + this.age + "岁了,所在班级" + this.className + "学号为" + this.stuId);
}
void playWith(String anotherName){
System.out.println(this.name+"正在和"+anotherName+"一起玩...");
}
//anotherName 前面不带this. 只有在方法的里面用
}