乱码问题

乱码问题

问题描述:

控制台打印出乱码,不知为何

相关截图:

图片描述

相关代码:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>s02</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
    </dependencies>

</project>

相关代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 在IoC容器启动时,自动由Spring实例化Apple对象,取名sweetApple放入到容器中 -->
    <bean id="girl-cloth" class="com.imooc.spring.ioc.entity.Clothes">
        <property name="style" value="连衣裙"></property>
        <property name="color" value="red"></property>
    </bean>

    <bean id="boy-cloth" class="com.imooc.spring.ioc.entity.Clothes">
        <property name="style" value="小西装"></property>
        <property name="color" value="blue"></property>
    </bean>

    <bean id="girl" class="com.imooc.spring.ioc.entity.Person">
        <property name="name" value="女孩"></property>
        <property name="clothes" ref="girl-cloth"></property>
    </bean>

    <bean id="boy" class="com.imooc.spring.ioc.entity.Person">
        <property name="name" value="男孩"></property>
        <property name="clothes" ref="boy-cloth"></property>
    </bean>

</beans>

相关代码:

package com.imooc.spring.ioc.entity;

public class Clothes {
       private  String style;
       private  String color;

    public Clothes() {
    }

    public Clothes(String style, String color) {
        this.style = style;
        this.color = color;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Clothes{" +
                "style='" + style + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}

相关代码:

package com.imooc.spring.ioc.entity;

public class Person {
    private String name;
    private Clothes clothes;

    public Person() {
    }

    public Person(String name, Clothes clothes) {
        this.name = name;
        this.clothes = clothes;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Clothes getClothes() {
        return clothes;
    }

    public void setClothes(Clothes clothes) {
        this.clothes = clothes;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", clothes=" + clothes +
                '}';
    }


    public void dress(){
        System.out.println(this.name+"爱穿"+this.clothes.getColor()+this.clothes.getStyle());
    }
}

相关代码:

package com.imooc.spring.ioc;


import com.imooc.spring.ioc.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

        Person girl = context.getBean("girl",Person.class);
        Person boy = context.getBean("boy",Person.class);

        girl.dress();
        boy.dress();
    }
}

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小蓝 2024-07-29 11:39:56

同学你好,从截图中看到,是“爱穿”两个字出现了乱码,其他位置都是没有问题的,那么猜测是Person类的编码出现了问题。建议同学对比一下Person和其他类的编码是否一致,编码在IDEA界面的右下角。如果不一致,将其都设置为UTF-8即可。

https://img1.sycdn.imooc.com/climg/66a70f0a0907b23702320066.jpg

祝学习愉快~

  • 提问者 慕桂英5534918 #1

    在设置--文件编码中将所有项设置成了UTF-8,person.java和SpringApplication.java的编码格式都是UTF-8 还是有乱码,为什么呢?

    2024-07-29 13:17:09
  • 提问者 慕桂英5534918 #2

    可以了,谢谢老师

    2024-07-29 13:22:33
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java工程师 2024版
  • 参与学习       2024    人
  • 提交作业       1318    份
  • 解答问题       1228    个

2024重磅革新,超百小时内容豪华升级,加速提升高级技能与高薪就业竞争力 课程紧贴企业最新人才需求,历经7年持续迭代,帮助万名学子入行转行 从零起点到高阶实战,学习路径稳健顺滑,成就从小白到工程师高薪

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师