小编jth*_*tht的帖子

覆盖java中的方法,然后将对象转换为父类行为

我有一个父类A,和一个子类,B和B覆盖了一个方法f,来自A.

public class A
{
    public String f()
    {
        return "A";
    }
}

public class B extends A
{
    ...
    public String f()
    {
        return "B";
    }

    public static void main(String[] args)
    {
        B b = new B();
        A a = (A) b;
        System.out.println(b.f()); //prints: B
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个类型为B,b的对象,并将其转换为类型A并将其分配给类型为A的变量a,然后在a上调用方法f.现在我希望调用父类的方法,因为我正在处理类型A的对象,但它没有,它调用方法的b版本(打印"B"而不是"A")下面的代码).

为什么会这样?这是设计决策还是技术限制?

java inheritance

9
推荐指数
1
解决办法
8956
查看次数

cpu如何决定将哪些数据放入哪个内存(ram,cache,register)?

当cpu正在执行程序时,它是否通过内存管道移动所有数据?然后,任何数据都将从ram-> cache->寄存器中移出,这样所有执行的数据都会在某个时刻进入cpu寄存器.或者它是以某种方式选择它放在那些更快的内存类型中的代码,还是作为程序员选择你想要保留的特定代码,例如,用于优化的缓存?

memory cpu computer-architecture

6
推荐指数
1
解决办法
2383
查看次数

在for循环中,每次迭代都会计算数组的长度吗?

如果我有一个for循环像...

for (int i = 0; i < myArray.length; i++) { ... }
Run Code Online (Sandbox Code Playgroud)

... myArray.length每次迭代都会得到评估吗?那会像......

int len = myArray.length;
for (int i = 0; i < len; i++) { ... }
Run Code Online (Sandbox Code Playgroud)

...性能小幅提升?

java

6
推荐指数
2
解决办法
1266
查看次数

将 Spring Boot Web 应用程序连接到 postgresql 服务器

我正在使用 Spring Boot 制作一个 Web 应用程序,我通过使用内存数据库(H2)获得了我想要的功能,但我无法将它连接到我在计算机上设置的 postgresql 服务器。我在这方面已经有一段时间了,并尝试了很多不起作用的东西,所以我将所有东西都设置为让它再次工作的方式。

这是我的 UploadController.java,它处理来自服务器的上传并将其放入我的内存数据库中:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import project.service.MediaFile;
import project.service.MediaFileRepository;

@Controller
public class UploadController {

    @Autowired
    private MediaFileRepository repository;

    @RequestMapping(value = "/uploadmedia", method = RequestMethod.GET)
    public String uploadForm() {
        return "upload";
    }

    @RequestMapping(value = "/uploadmedia", method = RequestMethod.POST)
    public String uploadSubmit(@RequestParam(value="files[]") MultipartFile[] files,
                               @RequestParam("tags") String tags, @RequestParam("type") String type)
    {
        String[] tagsArray = tags.split("\\s+");
        MultipartFile file;
        String name; …
Run Code Online (Sandbox Code Playgroud)

java postgresql jpa spring-mvc spring-boot

4
推荐指数
1
解决办法
2万
查看次数

Flask 如何从会话 cookie 中取回值?

我正在查看通过烧瓶中的会话对象设置的 cookie,它只是一个散列(或者看起来只是一个散列)。我为 cookie 设置的值无处可寻,但 Flask 可以取回该值。

我认为您必须将值与哈希值一起存储在 cookie 中(然后在您的应用程序中有一个 SECRET 以与哈希值混合),否则我不知道如何取回您的值。

我想也许他们会散列秘密和值,然后对其进行编码以进行额外的混淆或类似的事情。

python cookies hash flask

3
推荐指数
1
解决办法
1226
查看次数

使用Spring-Boot和Spring-MVC从文件系统中的任何位置提供文件

我正在尝试创建一个Web应用程序,您可以在其中上传保存在我的文件系统中的图像,然后在某个地址(有点像imgur)提供给用户.

在将图像上传并保存到我的系统后,我一直在处理图像时遇到麻烦.有人向我指出,当我将它们存储在我的项目目标文件夹中时,问题可能是我将图像存储为源树的一部分.现在,我首先将我的图像存储在project/src/main/resources/static/images中的原因是因为我无法在其他任何地方在我的网站上提供静态内容.

我一直在搜索和阅读这几天,我还没有找到任何适用的解决方案.这些问题的答案通常涉及我的项目没有的文件(比如application-servlet.xml或web.xml)以及我之前从未使用过的文件(我是Spring,Spring-Boot和一般的Java Web开发).

我的项目的基础是由github上的一个导师制作的,然后我克隆了它,包括Application.java,所以我用Spring Configuration看到的解决方案也不合适.

以下是我的项目(希望是相关的)部分:

这是我的Application.java文件.我没有做任何事情,它与我克隆的原始github中的文件完全相同.

package project;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

/**
 * The main class of the project.
 * By running the main class of {@link Application} then you start the Spring Boot system
 */
@SpringBootApplication
public class Application extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder){
        return applicationBuilder.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的application.properties.这里我刚刚添加了数据库内容:

spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
multipart.maxFileSize=-1
debug=true

spring.datasource.url=jdbc:postgresql://localhost:5432/finaltest
spring.datasource.username=myusername
spring.datasource.password=mypassword
Run Code Online (Sandbox Code Playgroud)

这是我的pom.xml(我项目中唯一的xml文件).这里我只添加了数据库依赖项:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" …
Run Code Online (Sandbox Code Playgroud)

java spring jsp spring-mvc spring-boot

2
推荐指数
1
解决办法
5148
查看次数

将我的工作Haskell代码转换为适当的Haskell代码

我正试图进入haskell,所以我搞乱了,我写了自己的elem函数,但它看起来很笨重.什么是哈克尔的方法来实现这一目标?

elem' x xs = 
    if null xs 
        then False
        else 
            if x == (head xs)
                then True 
                else elem' x (tail xs)  
Run Code Online (Sandbox Code Playgroud)

haskell

1
推荐指数
2
解决办法
99
查看次数

Expressjs 应用程序,使用 websockets 进行聊天。为 websocket 服务器使用不同的端口?

我正在使用 node.js 的 Express 框架制作一个应用程序,该框架通过 http 提供 html 内容,并使用 websockets 实现聊天功能。我想知道如何才能同时完成这两件事。我的想法是使用不同的端口进行 websocket 连接(因此 http 请求将到达端口 3000,而 websocket 将在端口 3001 上连接),但我不知道这是否是一个好的解决方案。我特别担心部署到像 heroku 这样的东西,以及我是否可以为我的应用程序指定不同的端口。

port websocket node.js express

1
推荐指数
1
解决办法
1901
查看次数