/**
* Java中的类
* 可以在类中定义属性、方法
*/
public class Demo02StudentJava {
String id;
String name;
int age;
String clazz;
//构造方法 对属性进行初始化
//无参构造方法
public Demo02StudentJava(){
}
//重载构造方法
public Demo02StudentJava(String id,String name,int age){
this.id=id;
this.name=name;
this.age=age;
this.clazz="自由分班";
}
//重载构造方法
public Demo02StudentJava(String id,String name,int age,String clazz){
this.id=id;
this.name=name;
this.age=age;
this.clazz=clazz;
}
@Override
//重写父类的构造方法
public String toString() {
return "Demo02StudentJava{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public void printClazz(){
System.out.println(this.name+","+this.clazz);
}
public static void main(String[] args) {
//传入参数 创建对象 该对象会默认获得类中定义的属性和方法
Demo02StudentJava stu1 = new Demo02StudentJava();
Demo02StudentJava zs = new Demo02StudentJava("001", "zs", 20);
System.out.println(stu1);
System.out.println(zs);
Demo02StudentJava ls = new Demo02StudentJava("002", "ls", 21);
Demo02StudentJava ww = new Demo02StudentJava("003", "ww", 22,"文科一班");
ls.printClazz();
ww.printClazz();
}
}
Demo02StudentJava{id='null', name='null', age=0}
Demo02StudentJava{id='001', name='zs', age=20}
ls,自由分班
ww,文科一班
/**
* Scala中的类
* 面向对象:封装 继承 多态
* 也可以定义属性、方法
* {}默认的构造方法
*/
class Demo03StudentScala(id:String,name:String,age:Int) {
//定义属性即赋值
val _id:String=id
val _name:String=name
val _age:Int=age
var _clazz:String=_ //待会再赋值
// var _clazz:String="自由分班" //给默认值
//重载构造方法
def this(id:String,name:String,age:Int,clazz:String){
//第一行代码必须调用默认的构造方法
this(id,name,age)
println("重载构造方法")
this._clazz=clazz
}
override def toString(): String ={
return s"Demo03StudentScala(id=${_id},name=${_name},age=${_age})"
}
def printStuName():Unit={
println(_name)
}
def printClazz(): Unit ={
println(this._clazz)
}
}
object Demo03StudentScala{
def main(args: Array[String]): Unit = {
//创建对象
val zs: Demo03StudentScala = new Demo03StudentScala("001", "zs", 20)
val ls: Demo03StudentScala = new Demo03StudentScala("002", "ls", 21)
println(zs)
println(ls)
println(zs._name)
zs.printStuName()
ls.printStuName()
val ww: Demo03StudentScala = new Demo03StudentScala("003", "ww", 22)
val xm: Demo03StudentScala = new Demo03StudentScala("004", "xm", 23,"文科二班")
ww.printClazz()
xm.printClazz()
}
}
Demo03StudentScala(id=001,name=zs,age=20)
Demo03StudentScala(id=002,name=ls,age=21)
zs
zs
ls
重载构造方法
null
文科二班
class Person(id:String,name:String,age:Int) {
val _id:String=id
val _name:String=name
val _age:Int=age
def printName(): Unit ={
println(s"这是Person类中的对象,名字为$name")
}
def walk(): Unit ={
println("人类会行走")
}
}
class Teacher(id:String,name:String,age:Int,clazz:String) {
val _id: String = id
val _name: String = name
val _age: Int = age
val _clazz: String = clazz
def printName(): Unit = {
println(s"这是Teacher类中的对象,名字为$name")
}
def walk(): Unit ={
println("Teacher也属于Person,也会行走")
}
}
class Student(id:String,name:String,age:Int,gender:String,clazz:String) {
val _id: String = id
val _name: String = name
val _age: Int = age
val _gender:String=gender
val _clazz: String = clazz
def printName(): Unit = {
println(s"这是Student类中的对象,名字为$name")
}
def walk(): Unit ={
println("Student也属于Person,也会行走")
}
}
class Person(id:String,name:String,age:Int) {
val _id:String=id
val _name:String=name
val _age:Int=age
def printName(): Unit ={
println(s"这是Person类中的对象,名字为$name")
}
def walk(): Unit ={
println("人类会行走")
}
override def toString: String ={
return s"Person(${_id},${_name},${_age})"
}
}
//Scala在继承父类时,需要调用父类的构造方法
class Teacher(id:String,name:String,age:Int,clazz:String) extends Person(id,name,age) {
//对子类特有的属性做初始化
val _clazz: String = clazz
//父类中由同名的方法 需要加上override进行重写
override def printName(): Unit = {
println(s"这是Teacher类中的对象,名字为$name")
}
override def walk(): Unit ={
println("Teacher也属于Person,也会行走")
}
override def toString: String ={
return s"Teacher(${_id},${_name},${_age},${_clazz})"
}
}
//继承能够获得父类定义好的属性和方法
class Student(id:String,name:String,age:Int,gender:String,clazz:String) extends Person(id,name,age){
val _gender:String=gender
val _clazz: String = clazz
override def printName(): Unit = {
println(s"这是Student类中的对象,名字为$name")
}
override def walk(): Unit ={
println("Student也属于Person,也会行走")
}
def printGender(): Unit ={
println(this._gender)
}
override def toString: String ={
return s"Student(${_id},${_name},${_age},${_gender},${_clazz})"
}
}
object Demo04ScalaClassExtend{
//多态 父类引用指向子类对象
def PersonWalkTwice(person:Person): Unit ={
person.walk()
person.walk()
}
def TeacherWalkTwice(teacher: Teacher): Unit ={
teacher.walk()
teacher.walk()
}
def StudentWalkTwice(student: Student): Unit ={
student.walk()
student.walk()
}
def main(args: Array[String]): Unit = {
val p1: Person = new Person("001", "p1", 20)
println(p1)
println(p1._id)
p1.walk()
p1.printName()
val t1: Teacher = new Teacher("002", "t1", 21,"文科一班")
println(t1._clazz)
val s1: Student = new Student("003", "s1", 22, "男", "文科二班")
println(s1._gender)
s1.printGender()
PersonWalkTwice(p1)
TeacherWalkTwice(t1)
StudentWalkTwice(s1)
PersonWalkTwice(t1)
PersonWalkTwice(s1)
}
}
Person(001,p1,20)
001
人类会行走
这是Person类中的对象,名字为p1
文科一班
男
男
人类会行走
人类会行走
Teacher也属于Person,也会行走
Teacher也属于Person,也会行走
Student也属于Person,也会行走
Student也属于Person,也会行走
Teacher也属于Person,也会行走
Teacher也属于Person,也会行走
Student也属于Person,也会行走
Student也属于Person,也会行走
object Demo05CaseClass {
def main(args: Array[String]): Unit = {
val zs: CaseStudent = new CaseStudent("001", "zs", 20, "男", "文科一班")
println(zs.id)
println(zs.name)
println(zs.gender)
zs.age=21
println(zs.age)
println(zs)
}
}
/**
* 样例类
* 实现序列化接口 可以在网络中传输
* 所有的属性默认都是不可以修改的 如果需要修改 那么在定义的时候要使用var修饰 默认是由val修饰
* 会重写默认的toString方法 如果直接打印 会输出对应的所有的属性值
* 会自动给每个属性加上get、set(由var修饰的属性才会有)方法
*/
case class CaseStudent(id:String,name:String,var age:Int,gender:String,clazz:String)
001
zs
男
21
CaseStudent(001,zs,21,男,文科一班)
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo06WordCountJava {
public static void main(String[] args) throws IOException {
HashMap wordMap = new HashMap<>();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\projects\\bigdata16\\Scala\\data\\words.txt")));
String line="";
while ((line= bufferedReader.readLine())!=null){
String[] words = line.split(",");
for (String word :
words) {
if(wordMap.containsKey(word)) {
Integer value = wordMap.get(word);
wordMap.put(word,value+1);
}else {
wordMap.put(word,1);
}
}
}
bufferedReader.close();
//打印结果
Set> entrySet = wordMap.entrySet();
for (Map.Entry entry : entrySet) {
System.out.println(entry.getKey()+" : "+entry.getValue());
}
}
}
java : 20
spark : 10
hadoop : 10
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)